什么是$ C $下获得一个刷新的Facebook标记在一个Android应用程序? [英] What is the code for getting a refreshed Facebook token in an Android app?

查看:210
本文介绍了什么是$ C $下获得一个刷新的Facebook标记在一个Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序。随着Facebook的offline_access许可即将去precation我试图使用图形API扩​​展Facebook的令牌。

I am developing an Android app. With the impending deprecation of Facebook's offline_access permission I am trying to use the Graph API to extend the Facebook token.

        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

任何人都可以提供详尽code,演示了如何实现code以上成一个Android应用程序,并得到刷新Facebook的令牌?

Can anyone provide detailed code that demonstrates how to implement the code above into an android app and obtain the refreshed Facebook token?

感谢您的时间。

更新二:

进展(我认为)!

使用的完整URL与facebook的请求方法的结果基本URL被添加到URL的开始 因此,而不是

Using the full URL with the facebook request method results in the base URL being added to the the beginning of the URL So instead of

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;

不过我现在得到响应{错误:{消息:错误验证应用程序无效的应用程序ID,类型:OAuthException,code:190}}

However I am now getting the response {"error":{"message":"Error validating application. Invalid application ID.","type":"OAuthException","code":190}}

更新一:

下面是我我试过了。的code结束,也就是说的onComplete上listerner被调用,但响应中不包含新的接入令牌或者过期值。

Here is what I have I tried. The code completes, that is OnComplete on the listerner is called, BUT the response does not contain a new access token or expiry value.

    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 ...

Here is my version of 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

的code结束,也就是说在listerner的onComplete被调用,但响应中不包含新的接入令牌或者过期值。该反应是......

The code completes, that is OnComplete on the listerner is called, BUT the response does not contain a new access token or expiry value. The response is...

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

当我把相同的URL(用字母当前令牌值)到网络浏览器的响应还带有一个访问令牌。

When I put the same URL (with a alphanumeric current token value) into a web browser the response DOES include an access token.

相关信息

Facebook的offline_access同意将pcated于5月1日去$ P $,2012

Facebook's offline_access permission will be deprecated on 1st of May, 2012

请不要建议使用onResume的extendAccessTokenIfNeeded功能()来代替。 <一href="http://stackoverflow.com/questions/10073426/is-it-possible-to-extend-facebook-tokens-with-extendaccesstokenifneeded-in-an-an">I我也遇到了麻烦的与,​​这是为什么我寻找到的图形API令牌提神的原因: - )

Please don't suggest using the extendAccessTokenIfNeeded function in onResume() instead. I am also having trouble with that and it is the reason why I am looking into the Graph API token refreshing :-)

相关堆栈溢出的问题

Related Stack Overflow Questions

<一个href="http://stackoverflow.com/questions/10073426/is-it-possible-to-extend-facebook-tokens-with-extendaccesstokenifneeded-in-an-an">Is它可以扩展Facebook的令牌与extendAccessTokenIfNeeded在一个Android应用程序?(我的问题)

<一个href="http://stackoverflow.com/questions/9930311/how-would-offline-access-work-after-de$p$pcation-after-may-1st">How 5月1日之后将offline_access后去precation工作?

Facebook的访问令牌不能扩展

<一个href="http://stackoverflow.com/questions/9549689/protecting-app-secret-for-extendaccesstoken-usage-java-android">Protecting应用程序秘密extendAccessToken使用(Java / Android的)

有关Facebook的链接

Relevant Facebook links

Facebook的Andr​​oid的教程

Facebook的offline_access权限去precation

推荐答案

说实话,我有点糊涂了 - 看起来你拥有了一切的这一切 - 这是简单的。但是,让我试着回答你的问题。下面是我的C#项目中的code,其中我对应用程序的令牌我的意见的情况下,你不熟悉C#语言和类:

To be honest, I'm a bit confused - looks like you have everything to get it done - and it's simple. But let me try to answer your question. Here is the code from my C# project where I extend app's token with my comments in case you are not familiar with C# languages and classes:

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应该是简单的。

hope that helps, and translating this into Java should be straightforward.

这篇关于什么是$ C $下获得一个刷新的Facebook标记在一个Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆