8

我正在开发一个 Android 应用程序。随着 Facebook 的离线访问权限即将被弃用,我正在尝试使用 Graph API 来扩展 Facebook 令牌。

        https://graph.facebook.com/oauth/access_token?             
        client_id=APP_ID&
        client_secret=APP_SECRET&
        grant_type=fb_exchange_token&
        fb_exchange_token=EXISTING_ACCESS_TOKEN

谁能提供详细的代码来演示如何将上面的代码实现到android应用程序中并获取刷新的Facebook令牌?

感谢您的时间。

更新二:

进步(我认为)!

将完整 URL 与 facebook 请求方法一起使用会导致将基本 URL 添加到 URL 的开头所以而不是

String refreshUrl = "https://graph.facebook.com/oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken;

应该使用

String refreshUrl = "oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken;

但是我现在收到响应 {"error":{"message":"Error validating application. Invalid application ID.","type":"OAuthException","code":190}}

更新一:

这是我尝试过的。代码完成,即调用侦听器上的 OnComplete,但响应不包含新的访问令牌或到期值。

    void refreshWithGraph() {
        AsyncFacebookRunner extendingAsyncRunner = new AsyncFacebookRunner(facebook);
        Bundle parameters = new Bundle();

        //the variable currentAccessToken is obtained after authorisation is complete using currentAccessToken = facebook.getAccessToken();

        String refreshUrl = "https://graph.facebook.com/oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken;


    extendingAsyncRunner.request(refreshUrl, parameters, new RefreshListener(), null );
   }

这是我的 RefreshListener 版本...

  //REFRESH LISTENER
    public class RefreshListener extends BaseRequestListener {

        public void onComplete(final String response, Object state) {


            try {
               final JSONObject json = Util.parseJson(response);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {             
                        tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE\nResponse is " + response);
                        tvRefreshToken.setText("IN REFRESH LISTENER ONCOMPLETE\nToken is " + facebook.getAccessToken());
                        tvRefreshExpiry.setText("IN REFRESH LISTENER ONCOMPLETE\nFacebook expiry is " + millisecToDate(facebook.getAccessExpires()));

                    }

                }); //end runOnUiThread



            } catch (JSONException e) {
                runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE CAUGHT JSON EXCEPTION \nResponse is " + response);

                            }

                         }); //end runOnUiThread

            } catch (FacebookError fe) {
                runOnUiThread(new Runnable() {


                            @Override
                            public void run() {
                                tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE CAUGHT FACEBOOK ERROR \nResponse is " + response);

                            }

                         }); //end runOnUiThread
            } //end catch Facebook error

        }  //end onComplete


        @Override
        public void onIOException(IOException e, Object state) {
            tvRefreshResponse.setText("IN REFRESH LISTENER IOEXCEPTION \nException is "+ e.getLocalizedMessage());

        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) {
            tvRefreshResponse.setText("IN REFRESH LISTENER FILE NOT FOUND EXCEPTION \nException is "+ e.getLocalizedMessage());
        }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) {
            tvRefreshResponse.setText("IN REFRESH MALFORMED URL \nException is "+ e.getLocalizedMessage());

        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            tvRefreshResponse.setText("IN REFRESH ONFACEBOOK ERROR \nException is "+ e.getLocalizedMessage());

        }
    } //end RefreshListener

代码完成,即调用侦听器上的 OnComplete,但响应不包含新的访问令牌或到期值。回应是...

 {"id":"https:\/\/graph.facebook.com\/oauth\/access_token","shares":78,"comments":1}

当我将相同的 URL(带有字母数字当前令牌值)放入 Web 浏览器时,响应确实包含访问令牌。


相关信息

Facebook 的 offline_access 权限将于 2012 年 5 月 1 日弃用

请不要建议在 onResume() 中使用 extendAccessTokenIfNeeded 函数。我也遇到了麻烦,这就是我正在研究 Graph API 令牌刷新的原因:-)

相关堆栈溢出问题

是否可以在 Android 应用程序中使用 extendAccessTokenIfNeeded 扩展 Facebook 令牌?(我的问题)

在 5 月 1 日后弃用后,offline_access 将如何工作?

Facebook 访问令牌无法扩展

保护用于 extendAccessToken 使用的应用程序机密 (Java/Android)

相关 Facebook 链接

Facebook 安卓教程

Facebook offline_access 权限弃用

4

1 回答 1

7

老实说,我有点困惑——看起来你有一切可以完成它——而且很简单。但让我试着回答你的问题。这是我的 C# 项目中的代码,如果您不熟悉 C# 语言和类,我会在其中使用我的注释扩展应用程序的标记:

string currentToken = "token from somewhere";

// WebClient is used to send GET request to the given URL
// and receive the response
using (var req = new System.Net.WebClient())
{
    // create URL string and fill it in with data (app Id, secret, and current token)
    var extendTokenUrl = string.Format(
        "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=fb_exchange_token&fb_exchange_token={2}",
        FB_APP_ID,
        FB_APP_SECRET,
        currentToken);

    // send GET request and download the response
    var response = req.DownloadString(extendTokenUrl);

    // if all is good, response will be a string which looks like this:
    // access_token=<<THE TOKEN GOES HERE>>
    var newToken = response.Substring("access_token=".Length);

    // now save newToken in your DB INSTEAD of currentToken - 
    // so all calls will be made with extended token
    SaveTokenInDB(newToken);
}

希望对您有所帮助,并且将其翻译成 Java 应该很简单。

于 2012-04-13T21:15:04.330 回答