问题描述
我正在OAuth1.0授权过程中,这是Spring引导中的Java代码,
package com.example.demo;
import com.mgiorda.oauth.OAuthConfig;
import com.mgiorda.oauth.OAuthConfigBuilder;
import com.mgiorda.oauth.OAuthSignature;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
//import com.example.demo.testsign;
import java.util.Collections;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.controller"})
@Configuration
@EnableAutoConfiguration
public class TestapiApplication {
public static String testm() {
OAuthConfig oauthConfig = new OAuthConfigBuilder("CONSUMER KEY","CONSUM_SECRET_KEY")
.setTokenKeys("ACCESS TOKEN","SECRET TOKEN")
.build();
OAuthSignature signature = oauthConfig.buildSignature(com.mgiorda.oauth.HttpMethod.GET,url)
.addQueryParam("aParam","aValue")
.addFormUrlEncodedParam("myParam","anotherValue")
.create();
return signature.getAsHeader();
}
public static void main(String[] args) {
String val = testm();
//System.out.println(val);
// request url
String url = "htpps://khjfskdhf.com";
// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();
// create headers
HttpHeaders headers = new HttpHeaders();
// set `Content-Type` and `Accept` headers
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// example of custom header
headers.set(HttpHeaders.AUTHORIZATION,val);
System.out.println(headers);
// build the request
httpentity request = new httpentity(headers);
// make an HTTP GET request with headers
ResponseEntity<String> response = restTemplate.exchange(
url,HttpMethod.GET,request,String.class
);
// check response
if (response.getStatusCode() == HttpStatus.OK) {
System.out.println("Request Successful.");
System.out.println(response.getBody());
} else {
System.out.println("Request Failed");
System.out.println(response.getStatusCode());
}
}
}
我已经使用签名方法创建了标头,并将其传递给我的main方法以获得授权。但是我收到404 Not Found错误,这是错误消息,
08:13:17.464 [main] DEBUG org.springframework.web.client.RestTemplate - Response 404 NOT_FOUND
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found: [no body]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:113)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:184)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583)
at com.example.demo.TestapiApplication.main(TestapiApplication.java:61)
谁能让我知道为什么它对我不起作用。我真的很难完成这个工作。有什么建议吗?
谢谢, 般若
解决方法
我通过提供URL参数值解决了该问题,并且还为标头创建了自定义唱歌请求方法。