Mel*_*Mel 8 android facebook facebook-android-sdk facebook-access-token
我正在开发一款Android应用.随着Facebook的offline_access权限即将弃用,我试图使用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
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供详细的代码,演示如何将上述代码实现到Android应用程序并获取刷新的Facebook令牌?
感谢您的时间.
更新二:
进步(我想)!
使用带有facebook请求方法的完整URL会导致基本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;
Run Code Online (Sandbox Code Playgroud)
应该用
String refreshUrl = "oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken;
Run Code Online (Sandbox Code Playgroud)
但是我现在收到响应{"error":{"message":"验证应用程序时出错.应用程序ID无效.","类型":"OAuthException","code":190}}
更新一:
这是我尝试过的.代码完成,即listerner上的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 );
}
Run Code Online (Sandbox Code Playgroud)
这是我的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
Run Code Online (Sandbox Code Playgroud)
代码完成,即listerner上的OnComplete被调用,但响应不包含新的访问令牌或到期值.回应是......
{"id":"https:\/\/graph.facebook.com\/oauth\/access_token","shares":78,"comments":1}
Run Code Online (Sandbox Code Playgroud)
当我将相同的URL(带有字母数字当前令牌值)放入Web浏览器时,响应包括访问令牌.
相关信息
Facebook的offline_access权限将于2012年5月1日弃用
请不要建议使用onResume()中的extendAccessTokenIfNeeded函数.我也遇到了麻烦,这就是为什么我要调查图谱API令牌的原因:-)
相关的Stack Overflow问题
是否可以在Android应用中使用extendAccessTokenIfNeeded扩展Facebook令牌?(我的问题)
为extendAccessToken使用保护app秘密(Java/Android)
相关的Facebook链接
说实话,我有点困惑 - 看起来你有一切都可以完成 - 而且很简单.但是,让我试着回答你的问题.以下是我的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);
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助,并将其转换为Java应该是直截了当的.
| 归档时间: |
|
| 查看次数: |
3304 次 |
| 最近记录: |