gpt4 book ai didi

android - 在 Android 应用程序中获取刷新的 Facebook token 的代码是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:51:21 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序。随着 Facebook 的 offline_access 权限即将弃用,我正在尝试使用图形 API 来扩展 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

任何人都可以提供详细的代码来演示如何将上述代码实现到 android 应用程序中并获取刷新的 Facebook token 吗?

感谢您的宝贵时间。

更新二:

进步(我认为)!

将完整 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,但响应不包含新的访问 token 或过期值。

    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,但响应不包含新的访问 token 或过期值。响应是...

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

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


相关信息

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

请不要建议在 onResume() 中使用 extendAccessTokenIfNeeded 函数。 I am also having trouble,这就是我正在研究 Graph API token 刷新的原因:-)

相关堆栈溢出问题

Is it possible to extend Facebook tokens with extendAccessTokenIfNeeded in an Android app?(我的问题)

How would offline_access work after deprecation after May 1st?

Facebook access token can not be extended

Protecting app secret for extendAccessToken usage (Java/Android)

相关 Facebook 链接

Facebook Android Tutorial

Facebook offline_access permission deprecation

最佳答案

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

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

关于android - 在 Android 应用程序中获取刷新的 Facebook token 的代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com