适用于四方的OAuth API

时间:2010-03-02 12:36:28

标签: java oauth foursquare

有没有人使用任何优秀的Java API用于四方面oauth?我正在寻找线程安全的东西?

干杯,

2 个答案:

答案 0 :(得分:1)

我首先使用foursquare4j,但无法成功检索访问令牌,这是调用Foursquare API签名调用之前所需的最后一步。 所以我切换到使用signpost来获取身份验证位并获取访问令牌和秘密,然后使用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();
        }
    }
}

答案 1 :(得分:0)

嗨,安德鲁我差不多为超酷的Play框架设置了一个模块,通过OAuth与Foursquare交流。

您可以在我的博客http://geeks.aretotally.in或Github https://github.com/feliperazeek/playframework-foursquare上关注进度。

请注意,因为Foursquare很快就会关闭OAuth 1以支持OAuth 2。