简体   繁体   English

使用 facebook 登录:从哪里获取访问令牌

[英]Login with facebook: where to get access token

I have 'app-id' and 'app-secret' but I am unable to find it that where can I get access token.我有 'app-id' 和 'app-secret',但我找不到它在哪里可以获得访问令牌。 Here is code I'am using:这是我正在使用的代码:

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.10',
  ]);

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];
// OR
// echo 'Name: ' . $user->getName();

official documentation 官方文件

Same question is asked here but no answer there, only link of documentation is there and in that documentation there is no mention that where to get access-token. 这里同样的问题但那里没有答案,只有文档链接,并且在该文档中没有提到从哪里获取访问令牌。

PS: I am trying to get user information using php. PS:我正在尝试使用 php 获取用户信息。

Edit: I've also tried to achieve it without access-token like this $response = $fb->get('/me?fields=id,name');编辑:我也尝试在没有像这样的访问令牌的情况下实现它$response = $fb->get('/me?fields=id,name'); but no luck yet.但还没有运气。

Please go to your app and follow below steps.请转到您的应用程序并按照以下步骤操作。

Open https://developers.facebook.com and click on "tools" then click on "Graph API".

For more information please refer below link

https://www.slickremix.com/facebook-60-day-user-access-token-generator/ https://www.slickremix.com/facebook-60-day-user-access-token-generator/

<a href="JavaScript:void(0);" onClick="FacebookLogin(); return false;">
    <img src="images/fb-login.png" class="img-responsive"/>
    <span>Facebook</span>
</a>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '',
            cookie: true,
            xfbml: true,
            version: 'v2.7'
        });
    };
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    function FacebookLogin() {
        FB.login(function (response) {
            if (response.authResponse) {
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me?scope=email&fields=last_name,first_name,email,gender', function (response) {
                    console.debug(response);
                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        });
     }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM