php 如何获取用户访问令牌?

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

How to get user access token?

phpfacebookfacebook-graph-apiaccess-tokenfacebook-access-token

提问by Moon

I'm trying to access scores through the Facebook API to get a list of scores of my friends. When I try to access /[facebook id]/scores, I get the following error even with my app access token:

我正在尝试通过 Facebook API 访问分数以获取我朋友的分数列表。当我尝试访问时/[facebook id]/scores,即使使用我的应用程序访问令牌,我也会收到以下错误:

FacebookApiException [ 0 ]: A user access token is required to request this resource. ~ APPPATH/classes/basefacebook.php [ 1039 ]

FacebookApiException [ 0 ]:请求此资源需要用户访问令牌。~ APPPATH/classes/basefacebook.php [ 1039 ]

How do I get a user access token?

如何获取用户访问令牌?

Updated:After reading http://developers.facebook.com/docs/reference/api/permissions/, I've confirmed that the app access token and user access token are two different access tokens. The question still remains, how do I get a user access token?

更新:阅读http://developers.facebook.com/docs/reference/api/permissions/ 后,我确认应用程序访问令牌和用户访问令牌是两个不同的访问令牌。问题仍然存在,我如何获得用户访问令牌?

采纳答案by ifaour

I see that you are using the php-sdk. So you need your friend to access your app with a script something like the example:

我看到您正在使用 php-sdk。因此,您需要您的朋友使用类似于示例的脚本访问您的应用程序:

<?php
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '191149314281714',
  'secret' => '73b67bf1c825fa47efae70a46c18906b',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

Then you need to:

那么你需要:

  1. Grant the offline_accesspermission
  2. Store the returned access_token
  3. Use that token whenever you query your friend's scores $facebook->api('friend_id/scores?access_token=$stored_access_token');
  1. 授予offline_access权限
  2. 存储返回的 access_token
  3. 每当您查询朋友的分数时使用该令牌 $facebook->api('friend_id/scores?access_token=$stored_access_token');

回答by Luke

Please refer to the documentations showing the authentication flows for Facebook.

请参阅显示 Facebook 身份验证流程的文档。

http://developers.facebook.com/docs/authentication/

http://developers.facebook.com/docs/authentication/

This is the best place to start to understand how it works.

这是开始了解其工作原理的最佳位置。

  1. You get the user's permission and access_token
  2. You can use this access_token as a "key" to get and post Facebook information
  1. 你得到用户的权限和access_token
  2. 您可以将此 access_token 作为“密钥”来获取和发布 Facebook 信息

The access_token is not to be mistaken for your application secret code, which is used to authorised to make a connection on behalf of your registered application.

access_token 不会被误认为是您的应用程序密码,该密码用于授权代表您注册的应用程序进行连接。

回答by melli-182

In Flex you can use:

在 Flex 中,您可以使用:

FacebookDesktop.getSession().accessToken;

回答by Hyman Marchetti

I call this function:

我调用这个函数:

FB.getLoginStatus(onFacebookInitialLoginStatus);

Which calls the function below and gets the accesss_token from the response.

它调用下面的函数并从响应中获取 accesss_token。

function onFacebookInitialLoginStatus(response) {
            if (response.status == "connected" && response.session) {
                var access_token = response.session.access_token;
                var url = 'https://graph.facebook.com/me/likes?access_token=' + access_token;
            }
            else {
                facebookLogin();
            }
        }

回答by Lalo

Suscribe to this event:

订阅此事件:

FB.Event.subscribe('auth.authResponseChange', function(response) {

then check response.status

然后检查 response.status