如何通过使用 Spring 引导的自定义 header 使用 OAuth1.0 验证 API
How to authenticate an API using OAuth1.0 with custom made header using Spring boot
我正在处理 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());
}
}
}
我使用签名方法创建了一个 header 并将其传递给我的主要方法以获得授权。但是我收到 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 参数值解决了这个问题,并为 header 创建了一个自定义歌唱请求方法。
我正在处理 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());
}
}
}
我使用签名方法创建了一个 header 并将其传递给我的主要方法以获得授权。但是我收到 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 参数值解决了这个问题,并为 header 创建了一个自定义歌唱请求方法。