【问题标题】:How to get user access token?如何获取用户访问令牌?
【发布时间】:2011-12-12 23:24:17
【问题描述】:

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

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

如何获取用户访问令牌?

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

【问题讨论】:

    标签: php facebook facebook-graph-api access-token facebook-access-token


    【解决方案1】:

    在 Flex 中你可以使用:

    FacebookDesktop.getSession().accessToken;
    

    【讨论】:

      【解决方案2】:

      订阅此活动:

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

      然后检查 response.status

      【讨论】:

        【解决方案3】:

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

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

        这是了解其工作原理的最佳起点。

        1. 你获得了用户的权限和access_token
        2. 您可以使用此 access_token 作为“密钥”来获取和发布 Facebook 信息

        不要将access_token误认为是您的应用密码,用于授权代表您注册的应用进行连接。

        【讨论】:

          【解决方案4】:

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

          <?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>
          

          那么你需要:

          1. 授予offline_access 权限
          2. 存储返回的access_token
          3. 在您查询朋友的分数时使用该令牌$facebook-&gt;api('friend_id/scores?access_token=$stored_access_token');

          【讨论】:

          • // 谢谢你。我添加了“离线范围”,但如何访问令牌?我使用了 $Facebook->getAccessToken(),但是没有用。 $Facebook 是 PHP 中 facebook sdk 的一个实例。
          • 您需要将offline_access 添加到您的范围内,然后一旦您的朋友连接到您的应用(在if($user) 内),调用您提到的函数将返回您需要存储的access_token!
          • // 谢谢!!现在可以了。我认为需要“offline_access”才能使其正常工作。
          • offline_access 权限已弃用,很快将被删除。当这个线程出现时它仍然有效,但即使那样你也不需要它。永远不会要求用户提供比您真正需要的更多的权限。抱歉,这是一个非常糟糕的答案,并且在步骤 3 中对 php sdk 的使用也不正确...
          • 这不是你真正的应用秘密,是吗?如果是这样,请务必更改应用密码。
          【解决方案5】:

          我调用这个函数:

          FB.getLoginStatus(onFacebookInitialLoginStatus);
          

          调用下面的函数并从响应中获取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();
                      }
                  }
          

          【讨论】:

          • 它在控制台中显示 onFacebookInitialLoginStatus 未定义
          猜你喜欢
          • 1970-01-01
          • 2014-05-05
          • 2016-02-05
          • 2017-01-28
          • 2012-05-16
          • 2016-04-20
          • 2021-06-18
          • 1970-01-01
          • 2022-01-07
          相关资源
          最近更新 更多