Play Framework:获取foursquare访问令牌

时间:2013-11-06 18:46:05

标签: web-services playframework-2.0 foursquare

我正在尝试了解Play框架如何处理其Web服务调用。

这是Foursquare的指示(在1和2之后):

Your server will make a request for
https://foursquare.com/oauth2/access_token
    ?client_id=YOUR_CLIENT_ID
    &client_secret=YOUR_CLIENT_SECRET
    &grant_type=authorization_code
    &redirect_uri=YOUR_REGISTERED_REDIRECT_URI
    &code=CODE

The response will be JSON
{ access_token: ACCESS_TOKEN }

这是我的代码:

public static Result index() {
    String base = "http://foursquare.com/oauth2/authenticate";
    String clientId = "?client_id=***";
    String responseType = "&response_type=code";
    String redirectUri = "&redirect_uri=http://localhost:9000/code";
    return redirect(base + clientId + responseType + redirectUri);
}


public static Result code() {
        String base = "http://foursquare.com/oauth2/access_token";
        String clientId = "?client_id=***";
        String clientsecret = "&client_secret=***";
        String grantType = "&grant_type=authorization_code";
        String redirectUri = "&redirect_uri=http://localhost:9000/json";
        String code = "&code=" + request().getQueryString("code");

        return redirect(base + clientId + clientsecret + grantType
                + redirectUri + code);

    }

我有两件事我很好奇:

1)我被重定向到屏幕上显示访问令牌的页面:

网址:https://foursquare.com/oauth2/access_token?client_id=***&client_secret=***&grant_type=authorization_code&redirect_uri=http://localhost:9000/json&code=***

显示:

{"access_token":"***"}

我原本希望被重定向到http://localhost:9000/json,因为那是重定向uri ......

2)如何获取访问令牌?我尝试使用异步Web服务调用http://www.playframework.com/documentation/2.2.x/JavaWS,但我不能让它们工作。

我的尝试:

String feedUrl = base + clientId + clientsecret + grantType
                + redirectUri + code;

        final Promise<Result> resultPromise = WS.url(feedUrl).get().map(
                new Function<WS.Response, Result>() {
                    public Result apply(WS.Response response) {
                        return ok("Feed title:" + response.asJson().findPath("access_token").asText());
                    }
                }
        );

        return resultPromise;

这给了我一个例外:[ConnectException: Connection refused: no further information: foursquare.com/107.23.103.139:80 to http://foursquare.com/oauth2/access_token]

非常感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

这是我为获取foursquare access_token并向foursquare提交新搜索所做的课程:

public class Application extends Controller {

    static final String HOST_AUTHENTICATE = "foursquare.com/oauth2/authenticate";
    static final String HOST_ACCESS_TOKEN = "https://www.foursquare.com/oauth2/access_token";
    static final String CLIENT_ID = "***";
    static final String REDIRECT_URI = "http://localhost:9000/code";
    static final String CLIENT_SECRET = "***";
    static final String GRANT_TYPE = "authorization_code";
    static final String RESPONSE_TYPE = "code";
    static final String FOURSQUARE_API_FRIENDS = "https://api.foursquare.com/v2/users/self/friends";

    public static Result index() {
        String uri = new URIBuilder().setScheme("https").setHost(HOST_AUTHENTICATE)
                .setParameter("client_id", CLIENT_ID).setParameter("response_type", RESPONSE_TYPE)
                .setParameter("redirect_uri", REDIRECT_URI).toString();

        return redirect(uri);
    }

    public static Promise<Result> code() {
        WSRequestHolder authenticate = WS.url(HOST_ACCESS_TOKEN).setQueryParameter("client_id", CLIENT_ID)
                .setQueryParameter("client_secret", CLIENT_SECRET).setQueryParameter("grant_type", GRANT_TYPE)
                .setQueryParameter("redirect_uri", REDIRECT_URI)
                .setQueryParameter("code", request().getQueryString("code").toString());

        final Promise<Result> resultPromise = authenticate.get().flatMap(new Function<WS.Response, Promise<Result>>() {
            public Promise<Result> apply(WS.Response response) {
                JsonNode json = response.asJson();
                return WS.url(FOURSQUARE_API_FRIENDS).setQueryParameter("oauth_token", json.findPath("access_token").asText())
                        .setQueryParameter("v", "20131108").get().map(new Function<WS.Response, Result>() {
                            public Result apply(WS.Response response) {
                                return ok(response.asJson());
                            }
                        });
            }
        });

        return resultPromise;
    }

}