java 如何获取给定 App ID 和 Secret 的 FB 访问令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15489851/
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 19:46:23  来源:igfitidea点击:

How to get FB access token given App ID and Secret?

javafacebookrestfb

提问by amphibient

I am just learning restfb. I registered an app on the FB site and got an app ID and Secret keys. However, as per this tutorial, I am supposed to have an access token to initialize DefaultFacebookClient. I tried to follow the instructions there but none sufficiently explained how to get the access token out of the app ID and secret key.

我只是在学习restfb。我在 FB 网站上注册了一个应用程序并获得了一个应用程序 ID 和密钥。但是,根据本教程,我应该有一个访问令牌来初始化DefaultFacebookClient。我试图按照那里的说明进行操作,但没有人充分解释如何从应用程序 ID 和密钥中获取访问令牌。

Request https://graph.facebook.com/oauth/authorize?client_id=MY_API_KEY&redirect_uri=http://www.facebook.com/connect/login_success.html

Facebook will redirect you to http://www.facebook.com/connect/login_success.html?code=MY_VERIFICATION_CODE

Request https://graph.facebook.com/oauth/access_token?client_id=MY_API_KEY& redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=MY_APP_SECRET&code=MY_VERIFICATION_CODE

Facebook will respond with access_token=MY_ACCESS_TOKEN

I did exactly that but every time I get

我就是这样做的,但每次我得到

{
   "error": {
      "message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
      "type": "OAuthException",
      "code": 100
   }
}

Any idea what I am supposed to do to get an access token and start using the API?

知道我应该怎么做才能获得访问令牌并开始使用 API 吗?

回答by cpilko

It looks like the plugin authors assume that you will be authenticating a user with the Login Dialogor the JavaScript SDK.

看起来插件作者假设您将使用登录对话框JavaScript SDK对用户进行身份验证。

Both of these methods return a user access token you can use. You need your App_ID and Secret to initialize these. Most Facebook operations require a user access token.

这两种方法都返回一个您可以使用的用户访问令牌。你需要你的 App_ID 和 Secret 来初始化这些。大多数 Facebook 操作都需要用户访问令牌。

If you are making API calls solely to read public data, then you may be able to get away with an application access token. For this you can use the DefaultFacebookClient().obtainAppAccessToken() methodof RestFB.

如果您只是为了读取公共数据而进行 API 调用,那么您可以使用应用程序访问令牌。为此,您可以使用RestFBDefaultFacebookClient().obtainAppAccessToken() 方法

回答by Erik Wendel

I were having the same issue.

我遇到了同样的问题。

Adding a trailing slash to the redirect_uri-parameter solved the problem for me.

向 redirect_uri 参数添加尾部斜杠为我解决了这个问题。

Also, make sure you are using the URI for your own app and not a facebook URI when making the requests.

此外,请确保您在发出请求时为自己的应用程序使用 URI,而不是 Facebook URI。

E.g:

例如:

http://www.yoursite.com

didn't work for me while

对我不起作用,而

http://www.yoursite.com/

did.

做过。

回答by ed22

I am not sure if that's what you want, but anyway:

我不确定这是否是您想要的,但无论如何:

To get access token for users associated with an app, you can use the /accounts 'edge' (see here).

要获取与应用程序关联的用户的访问令牌,您可以使用 /accounts 'edge'(请参阅此处)。

Also, this site about access tokenssays that you can make calls to Graph API like that: https://graph.facebook.com/endpoint?access_token=app_id|app_secret

此外,这个关于访问令牌的网站说你可以像这样调用Graph API:https: //graph.facebook.com/endpoint?access_token=app_id|app_secret

Now, to combine that, to get the access tokens (in JSON format along with some other stuff): https://graph.facebook.com/{your app id here}/accounts?access_token={again, your app id here}|{your app secret here}

现在,结合起来,获取访问令牌(以 JSON 格式以及其他一些内容):https: //graph.facebook.com/{your app id here}/accounts?access_token={again, your app id here }|{您的应用机密在这里}

Please substitute the fields in the url above and remove all the { and } from it.

请替换上面 url 中的字段并从中删除所有 { 和 }。