这是我的:
static AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken("<my app id>", "<my app secret>");
static FacebookClient client = new DefaultFacebookClient();
public static void main(String args[]) {
System.out.print("Enter Your Status: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String status= null;
try {
userName = br.readLine();
System.out.println("..........");
} catch (IOException ioe) {
System.out.println("!");
System.exit(1);
}
FacebookType publishMessageResponse =
client.publish("me/feed", FacebookType.class,
Parameter.with("message", status));
所以第一行获取 token 并将其存储为 AccessToken 类型,但这对我有什么好处,因为下一行我需要以字符串形式提供访问 token ,但我无法转换它。有什么帮助吗?
最佳答案
首先不要混淆应用程序 token 和用户 token (more info)
要获取用户 token ,您必须
- 为用户提供一种方法来针对 Facebook (more info) 进行身份验证并接收“代码”——每个登录请求唯一的加密字符串。
- 使用此代码获取用户 token 。
您可以通过以下方式使用纯 RestFB 获取用户 token :
private FacebookClient.AccessToken getFacebookUserToken(String code, String redirectUrl) throws IOException {
String appId = "YOUR_APP_ID";
String secretKey = "YOUR_SECRET_KEY";
WebRequestor wr = new DefaultWebRequestor();
WebRequestor.Response accessTokenResponse = wr.executeGet(
"https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl
+ "&client_secret=" + secretKey + "&code=" + code);
return DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody());
}
调用很简单:
FacebookClient.AccessToken token = getFacebookUserToken(code, redirectUrl);
String accessToken = token.getAccessToken();
Date expires = token.getExpires();
关于java - RestFB:使用 Facebook 应用程序获取用户访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671694/