Java Facebook服务器到服务器获取代码URL参数

时间:2015-10-23 11:26:43

标签: java facebook oauth

我已经为此工作了几个星期......

基本上我要做的是登录Facebook(验证,接受权限等),解析返回"代码" URL查询参数,并使用" code"获取FB用户访问令牌的参数...

这是FB_OAuthURL(对于此示例,它是变量的名称):

https://www.facebook.com/dialog/oauth?client_id=<APP_ID>&redirect_uri=http%3A%2F%2Flocalhost%2Fconnect%2Flogin_success.html&scope=public_profile%2Cpublish_actions%2Cuser_about_me%2Cuser_actions.books%2Cuser_actions.fitness%2Cuser_actions.music%2Cuser_actions.news%2Cuser_actions.video%2Cuser_birthday%2Cuser_education_history%2Cuser_events%2Cuser_games_activity%2Cuser_hometown%2Cuser_religion_politics%2Cuser_status%2Cuser_tagged_places%2Cuser_work_history%2Crsvp_event%2Cuser_relationships%2Cuser_relationship_details%2Cuser_location%2Cuser_likes%2Cuser_posts&state=<RANDOM_NUMBER>

以下是我正在使用的方法

public static String getFinalRedirectedUrl(String url) {
        HttpURLConnection connection;
        String finalUrl = url;
        try {
            do {
                connection = (HttpURLConnection) new URL(finalUrl).openConnection();
                connection.setInstanceFollowRedirects(false);
                connection.setUseCaches(false);
                connection.setRequestMethod("GET");
                connection.connect();
                if (connection.getResponseCode() >= 300 && responseCode < 400) {
                    String redirectedUrl = connection.getHeaderField("Location");
                    if (null == redirectedUrl)
                        break;
                    finalUrl = redirectedUrl;
                    System.out.println("redirected url: " + finalUrl);
                } else
                    break;
            } while (connection.getResponseCode() != HttpURLConnection.HTTP_OK);
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalUrl;
    }

然而,结果(让我们称之为FB_REDIRCTURL)如下:

https://www.facebook.com/login.php?skip_api_login=1&api_key=<APP_ID>&signed_next=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.5%2Fdialog%2Foauth%3Fredirect_uri%3Dhttp%253A%252F%252Flocalhost%252Fconnect%252Flogin_success.html%26state%3D-<RANDOM_NUMBER>%26scope%3Dpublic_profile%252Cpublish_actions%252Cuser_about_me%252Cuser_actions.books%252Cuser_actions.fitness%252Cuser_actions.music%252Cuser_actions.news%252Cuser_actions.video%252Cuser_birthday%252Cuser_education_history%252Cuser_events%252Cuser_games_activity%252Cuser_hometown%252Cuser_religion_politics%252Cuser_status%252Cuser_tagged_places%252Cuser_work_history%252Crsvp_event%252Cuser_relationships%252Cuser_relationship_details%252Cuser_location%252Cuser_likes%252Cuser_posts%26client_id%3D<APP_ID>%26ret%3Dlogin&cancel_url=http%3A%2F%2Flocalhost%2Fconnect%2Flogin_success.html%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%26state%3D-4486902649550591089%23_%3D_&display=page

我的两个问题是

  1. 如果我复制/粘贴此网址 - 浏览器会重定向我,我会收到&#34;代码&#34; param - 再次与我手动复制&amp;粘贴 - 如何让方法继续前进并最终检索我正在寻找的http响应

  2. FB_REDIRCTURL说URL params中有一个错误,但正如我所说,当我复制&amp;将网址粘贴到浏览器中......任何想法都是为什么?

  3. 谢谢大家 - 我非常感谢帮助

0 个答案:

没有答案