gpt4 book ai didi

java - 如何使用 java 创建 oAuth 请求?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:17 25 4
gpt4 key购买 nike

我需要与 Viagogo website 建立联系使用 oAuth。引用他们的 documentation我需要创建一个类似于下面的请求

Using the example in step 1A, this means you may generate a signature base string that looks like the following:

GET&http%3A%2F%2Fapi.viagogo.net%2FPublic%2FSimpleOAuthAccessRequest&oauth_consumer_key%3Dtestkey%26oauth_nonce%3Dmyn0nc3%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1292404912%26oauth_version%3D1.0%26scope%3DAPI.Public

我正在使用以下代码,但是当我注释第 1,2 行时它返回未经授权的错误,当我使用它们时它显示 oauthService.signRequest 返回 void。

贸易王API.java

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;

public class TradeKingAPI extends DefaultApi10a {
@Override
public String getRequestTokenEndpoint() {
return "http://api.viagogo.net/Public/SimpleOAuthAccessRequest";
}
@Override
public String getAccessTokenEndpoint() {
return "http://api.viagogo.net/Public/SimpleOAuthAccessRequest";
}
@Override
public String getAuthorizationUrl(Token requestToken) {
return "http://api.viagogo.net/Public/SimpleOAuthAccessRequest";
}
}

主.java

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

import api.TradeKingAPI;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.OAuthConstants;
import org.scribe.oauth.OAuthService;

........

OAuthService oauthService = new ServiceBuilder()
.provider(TradeKingAPI.class)
.apiKey("My consumer key")
.apiSecret("My secret")
.scope("API.Public")
.build();

Long seconds = (System.currentTimeMillis() / 1000);
System.out.println(">>>" + seconds);
String stSeconds = seconds.toString();
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.viagogo.net/Public
/SimpleOAuthAccessRequest");

request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, "My consumer key");
request.addOAuthParameter(OAuthConstants.NONCE, "myn0nc3");
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, "HMAC-SHA1");
request.addOAuthParameter(OAuthConstants.TIMESTAMP, seconds.toString());
request.addOAuthParameter(OAuthConstants.VERSION, "1.0");
request.addOAuthParameter("scope", "API.Public");

1 String signature = oauthService.signRequest(OAuthConstants.EMPTY_TOKEN, request);

2 request.addOAuthParameter(OAuthConstants.SIGNATURE,signature);
Response response = request.send();
System.err.println(">>" + response.isSuccessful());
System.err.println(">>" + response.getMessage());
System.err.println(">>" + response.getBody());

最佳答案

据我从 Viagogo 公共(public) API 访问文档中了解到,您在第 1 步中获得的 token 等同于完整的 OAuth 1.0a“舞蹈”中的请求 token 。

因此,您应该能够使用 scribe-java 内部类来获取此 token ,而无需手动操作。唯一不同的是,在 scribe 中,这个请求还向 OAuth 服务器发送了一个回调 url,用于下一步的 OAuth“舞蹈”。

由于我无法获得消费者帐户,因此我只能在这里做出假设。那么让我们有两种情况:

场景 1:Viagogo 服务器容忍额外参数(即回调 URL)

所以你可以使用这段代码

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;

public class TradeKingAPI extends DefaultApi10a {

@Override
public Verb getRequestTokenVerb()
{
return Verb.GET;
}

@Override
public String getRequestTokenEndpoint() {
return "http://api.viagogo.net/Public/SimpleOAuthAccessRequest";
}

@Override
public String getAccessTokenEndpoint() {
return "none";
}

@Override
public String getAuthorizationUrl(Token requestToken) {
return "none";
}
}

那么您的调用代码将是:

OAuthService service = new ServiceBuilder()
.provider(TradeKingAPI.class)
.signatureType(QueryString)
.apiKey("My consumer key")
.apiSecret("My secret")
.scope("API.Public")
.build();

Token requestToken = service.getRequestToken();

//make your API calls

OAuthRequest request = new OAuthRequest(Verb.GET,
"http://api.viagogo.net/Public/Event/235");
service.signRequest(requestToken, request);
Response response = request.send();
System.out.println(response.getBody());

但正如我所说,如果 Viagogo 安全性有点严格并且它​​拒绝无用的参数 oauth_callback,您将需要切换到场景 2

场景 2:构建您自己的 OAuthService

在这种情况下,您必须创建一个新的 OAuthService 以避免处理 OAuthCallback 参数。

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.*;
import org.scribe.oauth.OAuth10aServiceImpl;

import java.util.Map;

public class OAuth10aServiceForViagogo extends OAuth10aServiceImpl {

private OAuthConfig config;
private DefaultApi10a api;

public OAuth10aServiceForViagogo(DefaultApi10a api, OAuthConfig config) {
super(api, config);

this.api = api;
this.config = config;
}

private void addOAuthParams(OAuthRequest request, Token token) {
request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce());
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));

}

private String getSignature(OAuthRequest request, Token token) {

String baseString = api.getBaseStringExtractor().extract(request);
String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());

return signature;
}

private void appendSignature(OAuthRequest request) {
for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet()) {
request.addQuerystringParameter(entry.getKey(), entry.getValue());
}
}

@Override
public Token getRequestToken(RequestTuner tuner) {
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());

addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
appendSignature(request);

Response response = request.send(tuner);
String body = response.getBody();

return api.getRequestTokenExtractor().extract(body);
}
}

TrakingApi 类在调用 createService 时创建 OAuth10aServiceForViagogo 会略有不同:

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;

public class TradeKingAPI extends DefaultApi10a {

@override
public OAuthService createService(OAuthConfig config)
{
return new OAuth10aServiceForViagogo(this, config);
}

@Override
public Verb getRequestTokenVerb()
{
return Verb.GET;
}

@Override
public String getRequestTokenEndpoint() {
return "http://api.viagogo.net/Public/SimpleOAuthAccessRequest";
}

@Override
public String getAccessTokenEndpoint() {
return "none";
}

@Override
public String getAuthorizationUrl(Token requestToken) {
return "none";
}
}

那么你的调用代码将是相同的:

    OAuthService service = new ServiceBuilder()
.provider(TradeKingAPI.class)
.signatureType(QueryString)
.apiKey("My consumer key")
.apiSecret("My secret")
.scope("API.Public")
.build();

Token requestToken = service.getRequestToken();

//make your API calls

OAuthRequest request = new OAuthRequest(Verb.GET,
"http://api.viagogo.net/Public/Event/235");
service.signRequest(requestToken, request);
Response response = request.send();
System.out.println(response.getBody());

我没有测试所有这些代码,因为我无法访问消费者和 key ,但它应该接近您的需要。

关于java - 如何使用 java 创建 oAuth 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17725802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com