简体   繁体   English

了解使用 PHP SDK 为网站 Facebook 提要生成访问令牌

[英]Understand the generation of access tokens for website Facebook feed using PHP SDK

My overall goal is to simply display posts from my Facebook page onto a website, but I feel like I'm missing something important, making this seemingly simple task quite difficult!我的总体目标是简单地将来自我的 Facebook 页面的帖子显示到网站上,但我觉得我错过了一些重要的东西,让这个看似简单的任务变得非常困难!

I have set up my app in Facebook Developer and generated a test access token via the Graph API Explorer to test out the example set out on https://developers.facebook.com/docs/reference/php/examples , shown below, which uses the PHP SDK.我已经在 Facebook Developer 中设置了我的应用程序并通过 Graph API Explorer 生成了一个测试访问令牌来测试https://developers.facebook.com/docs/reference/php/examples上列出的示例,如下所示,使用 PHP SDK。 This works fine.这工作正常。

<?php

require_once __DIR__ . '/vendor/autoload.php'; 

$fb = new \Facebook\Facebook([
  'app_id' => '{your-app-id}',
  'app_secret' => '{your-app-secret}',
  'graph_api_version' => 'v5.0',
]);

try {
  $response = $fb->get('/me?fields=name,hometown', '{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;
}

$me = $response->getGraphUser();
echo 'All the data returned from the server: ' . $me;
echo 'My name is ' . $me->getName();
$hometown = $me->getHometown();
$hometown_name = $hometown->getName();
echo 'My hometown is ' . $hometown_name;

?>

However, given that the access token will expire, I am under the impression I should therefore request a new token when someone visits the website (assuming the previous one has expired, and assuming I store it for use during the period it is active).但是,鉴于访问令牌将过期,我的印象是,当有人访问该网站时,我应该请求一个新令牌(假设前一个令牌已过期,并假设我将其存储以在其活动期间使用)。 Is this correct?这样对吗? If so, I am failing to figure out how to generate a token in these circumstances.如果是这样,我无法弄清楚如何在这些情况下生成令牌。 There are posts like this one from 7 years ago ( Facebook PHP SDK dealing with Access Tokens ) but can't seem to make anything work with the example I'm using (presumably because it is an older version of the SDK). 7 年前就有这样的帖子( Facebook PHP SDK 处理访问令牌),但似乎无法对我正在使用的示例进行任何操作(大概是因为它是旧版本的 SDK)。

Can anyone assist with how I generate a new token to allow my basic feed to work without manual creation of an access token?任何人都可以帮助我如何生成新令牌以允许我的基本提要工作而无需手动创建访问令牌? Surely this is just basic stuff but it is baffling me.当然,这只是基本的东西,但它让我感到困惑。 I have seen references to using the format '{your-app-id}|{your-app-secret}' to create an access token but this seems to create an app token that doesn't do the job.我已经看到使用格式 '{your-app-id}|{your-app-secret}' 来创建访问令牌的引用,但这似乎创建了一个不能完成这项工作的应用程序令牌。

Any help would be much appreciated!任何帮助将非常感激!

Thank you!谢谢!

I've been struggling with this in the past as well.过去我也一直在为此苦苦挣扎。 So don't worry :) The relevant part what you're looking for is a "Long-Lived Access Tokens".所以别担心 :) 您正在寻找的相关部分是“长期访问令牌”。 Here is the documentation to it:这是它的文档:

https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/ https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/

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

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