gpt4 book ai didi

java - 四方的 OAuth API

转载 作者:行者123 更新时间:2023-12-01 16:07:16 25 4
gpt4 key购买 nike

有人使用任何好的 Java API 来进行四方 oauth 吗?我正在寻找线程安全的东西?

干杯,

最佳答案

我首先使用 foursquare4j ,但无法让它成功检索访问 token ,这是对 Foursquare API 进行签名调用之前所需的最后一步。所以我改用 signpost通过身份验证位并获取访问 token 和 secret ,然后使用 foursquare4j,因为它将 API 封装为一个非常好的对象模型。我不知道路标或 foursquare4j 本质上是否是线程安全的 - 您可以在他们的论坛中提出这个问题。下面是我用来验证我的应用程序的代码的扁平化版本。我正在使用 Play 框架 -authorize() 接收初始身份验证请求,oauth() 是 foursquare 在用户允许访问后重定向到的位置。

public class Foursquare extends Controller {

static final String FOURSQUARE_OAUTH_REQUEST_TOKEN = "http://foursquare.com/oauth/request_token";
static final String FOURSQUARE_OAUTH_ACCESS_TOKEN = "http://foursquare.com/oauth/access_token";
static final String FOURSQUARE_OAUTH_AUTHORIZE = "http://foursquare.com/oauth/authorize";
static final String CONSUMER_KEY = "N4FKW2GFLMU1UGR3DDQYE4IGJQRGID1JFXYPJS3XFLZN3EU6";
static final String CONSUMER_SECRET = "DDGHBF25J3RJDD4N4QC2CMRF8YMA1CG94OGFRPTY4RQNLMVH";


// Handle Request for foursquare Authorization
public static void authorize() throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {

oauth.signpost.OAuthProvider provider = new DefaultOAuthProvider(FOURSQUARE_OAUTH_REQUEST_TOKEN, FOURSQUARE_OAUTH_ACCESS_TOKEN,FOURSQUARE_OAUTH_AUTHORIZE);
oauth.signpost.OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
String authURL;

authURL = provider.retrieveRequestToken(consumer,"");

String tokenSecret = consumer.getTokenSecret();
session.put("secret", tokenSecret);
redirect(authURL);
}

// Handle call back from foursquare after user Accepts
public static void oauth() throws IOException, OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {

oauth.signpost.OAuthProvider provider = new DefaultOAuthProvider(FOURSQUARE_OAUTH_REQUEST_TOKEN, FOURSQUARE_OAUTH_ACCESS_TOKEN,FOURSQUARE_OAUTH_AUTHORIZE);
oauth.signpost.OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
String secret = session.get("secret");
String pinCode = params.get("oauth_token");
consumer.setTokenWithSecret(pinCode, secret);
provider.retrieveAccessToken(consumer, pinCode);

// Get the access token and secret
String token = consumer.getToken();
String tokenSecret = consumer.getTokenSecret();

// Set foursquare4j Credentials
foursquare4j.type.Credentials credentials = new Credentials();
credentials.setTokenSecret(tokenSecret);
credentials.setAccessToken(token);

foursquare4j.oauth.OAuthConsumer newConsumer = new OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

foursquare4j.oauth.FoursquareOAuthImpl fs = new FoursquareOAuthImpl(newConsumer, credentials);

try {
// Get last 50 checkins
Checkins checkins = fs.history("50", "");

render(checkins);
} catch (FoursquareException e) {
e.printStackTrace();
}
}
}

关于java - 四方的 OAuth API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2363084/

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