无法让 facebook 通过 GAE 获取 access_token

发布于 2024-12-14 15:10:11 字数 2053 浏览 7 评论 0原文

当我尝试从 facebook 获取 access_token 时,收到状态代码 404。这是一个用 Java 编写的 GAE 应用程序,我正在以编程方式获取 access_token。也就是说,一旦我获得“代码”,我就会发出 https 请求来获取 access_token。我正在使用restlet.org 框架。

public void getAccessToken( final Map<String,String> filter ) {
    //prepare and issue the request. Send in the host and other data
    String url = Application.getInstance().getAccessTokenUrl( this.getReference().getHostIdentifier(), filter );

    Client client = new Client(Protocol.HTTPS);
    client.setConnectTimeout(5000);
    Request request = new Request(Method.GET, url);

    client.handle(request, new Uniform() {
        @Override
        public void handle(Request arg0, Response response) {
            if ( response.getStatus().isSuccess() ) {
                if ( response.isEntityAvailable() ) {  
                    String result = response.getEntityAsText();
                    String results[] = result.split("&");
                    String accessTokens[] = results[0].split("=");
                    filter.put("access_token", accessTokens[1]);
                }
            } else {
                // the user doesn't exist
                log.warning("getAccessToken, description - " + response.getStatus().getDescription() + ", code - " + response.getStatus().getCode() + ", all - " + response.getStatus().toString());
                setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, "Failed to get facebook access code."));
            }
        }
    });
}

其中code、client_id、client_secret 和redirectUrl 全部组合起来创建正确的http url。

当我从我的开发环境(192.168.1.../facebook/signin)本地运行它时,效果非常好。但是,当我在 GAE 上运行它时,它在尝试检索 access_token 时返回 404。我已经记录了 GAE 发送的 http 请求,它是正确的。也就是说,如果我插入请求( https://graph.facebook.com/oauth/ access_token?client_id=298631... )进入我的浏览器,它正确返回 access_token。我相信这证实了 Facebook 上的应用程序设置是正确的。

因此,当代码从我的开发系统执行时,它可以正确运行, 当我在浏览器中插入 GAE 发送的 URL 时,它会正确运行, 仅当 GAE 发送 http 请求时才会失败。

有人对这可能是什么或要寻找什么有任何建议吗?

I'm getting a status code of 404 when trying to get the access_token from facebook. The is a GAE app written in Java and I'm programmatically getting the access_token. That is, once I get the "code" I then make https request to get the access_token. I'm using the restlet.org framework.

public void getAccessToken( final Map<String,String> filter ) {
    //prepare and issue the request. Send in the host and other data
    String url = Application.getInstance().getAccessTokenUrl( this.getReference().getHostIdentifier(), filter );

    Client client = new Client(Protocol.HTTPS);
    client.setConnectTimeout(5000);
    Request request = new Request(Method.GET, url);

    client.handle(request, new Uniform() {
        @Override
        public void handle(Request arg0, Response response) {
            if ( response.getStatus().isSuccess() ) {
                if ( response.isEntityAvailable() ) {  
                    String result = response.getEntityAsText();
                    String results[] = result.split("&");
                    String accessTokens[] = results[0].split("=");
                    filter.put("access_token", accessTokens[1]);
                }
            } else {
                // the user doesn't exist
                log.warning("getAccessToken, description - " + response.getStatus().getDescription() + ", code - " + response.getStatus().getCode() + ", all - " + response.getStatus().toString());
                setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, "Failed to get facebook access code."));
            }
        }
    });
}

where code, client_id, client_secret and redirectUrl all combine to create the correct http url.

This works perfectly well when I run it locally from my dev environment (192.168.1.../facebook/signin). However, when I run it on GAE, it returns a 404 when trying to retrieve the access_token. I've logged the http request that GAE is sending and it is correct. That is, if I plug the request ( https://graph.facebook.com/oauth/access_token?client_id=298631... ) into my browser, it correctly returns the access_token. I believe this confirms that the app settings are correct on Facebook.

So, it runs correctly when the code executes from my dev system,
it runs correctly when I plug in the URL that GAE is sending in my browser,
it only fails when GAE sends the http request.

Does anyone have any suggestions about what this might be or what to look for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文