题
有没有人用任何好的Java API来四点方形的OAuth?我寻找的东西是线程安全的?
干杯,
解决方案
我开始用 foursquare4j ,但无法得到它成功地检索访问令牌,这是呼吁签署调用Foursquare的API之前所需的最后一步。 所以我切换到使用路标通过认证比特获得,并得到一个访问令牌和秘密,然后用foursquare4j因为它封装了API作为一个非常好的对象模型。 我不知道,如果路标或foursquare4j固有线程安全的 - 你可以问这个问题在他们的论坛。 下面是代码的扁平化版本,我使用的身份验证我的应用程序。 我使用Play框架 - 授权()接收用于认证的初始请求,和OAuth()是四角重定向到一旦用户已经允许访问
。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();
}
}
}
其他提示
嗨安德鲁,我几乎完成了建立一个模块的超爽游戏框架,通过OAuth的交谈四方。
您可以按照 http://geeks.aretotally.in 或Github上的 https://github.com/feliperazeek/playframework-foursquare 。
观看,因为四方以有利于的OAuth 2的很快关闭的OAuth 1。
不隶属于 StackOverflow