java RestFB:使用 Facebook 应用程序获取用户访问令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13671694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:33:26  来源:igfitidea点击:

RestFB: Using a facebook app to get the users Access Token

javafacebookrestfb

提问by Bevilacqua

This is what i have:

这就是我所拥有的:

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));

So first line gets the token and stores it as type AccessToken but what good does that do to me because next line i need to provide the access token as a string and i can't convert it. Any Help?

因此,第一行获取令牌并将其存储为 AccessToken 类型,但这对我有什么好处,因为下一行我需要将访问令牌作为字符串提供,而我无法转换它。任何帮助?

采纳答案by Kenny Cason

In addition to what Hyman said about AccessToken.getAccessToken() returning the string value of accessToken, you can avoid instantiating DefaultFacebookClienttwice by extending DefaultFacebookClientlike this:

除了 Hyman 所说的 AccessToken.getAccessToken() 返回 accessToken 的字符串值之外,您可以DefaultFacebookClient通过这样扩展来避免实例化两次DefaultFacebookClient

import com.restfb.DefaultFacebookClient;

public class LoggedInFacebookClient extends DefaultFacebookClient {

    public LoggedInFacebookClient(String appId, String appSecret) {
        AccessToken accessToken = this.obtainAppAccessToken(appId, appSecret);
        this.accessToken = accessToken.getAccessToken();
    }

}

回答by Val

First of all don't confuse app token with user token (more info)

首先不要将应用程序令牌与用户令牌混淆(更多信息

To get user token you have to

要获取用户令牌,您必须

  1. Provide a way for user to authenticate against Facebook (more info) and receive "code" - an encrypted string unique to each login request.
  2. Get the user token using this code.
  1. 为用户提供一种方式来对 Facebook 进行身份验证(更多信息)并接收“代码”——每个登录请求唯一的加密字符串。
  2. 使用此代码获取用户令牌。

You can get user token with pure RestFB the following way:

您可以通过以下方式使用纯 RestFB 获取用户令牌:

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());
}

The call is simple:

调用很简单:

FacebookClient.AccessToken token = getFacebookUserToken(code, redirectUrl);
String accessToken = token.getAccessToken();
Date expires = token.getExpires();

回答by user2492472

Try the following code:

试试下面的代码:

AccessToken accessToken = new DefaultFacebookClient().obtainAppAccessToken(appid,appsecret);
String token=accessToken.getAccessToken();

回答by Hyman White

Per restfb.FacebookClient.AccessToken, you should be able to call accessToken.getAccessToken() -- that should return the String you are looking for.

根据restfb.FacebookClient.AccessToken,您应该能够调用 accessToken.getAccessToken() - 它应该返回您正在寻找的字符串。

回答by Dilip Bobby

This will work

这将工作

AccessToken accessToken = new DefaultFacebookClient().obtainAppAccessToken("XXXX", "XXXX");
String token=accessToken.getAccessToken();
DefaultFacebookClient facebookClient = new DefaultFacebookClient(token);