[英]Retrieving facebook user access token with socialite on laravel 5.4
I am trying to get user access token so I can send CURL request through Guzzle to Facebook's Graph API to get user's friends that are using the app.我正在尝试获取用户访问令牌,以便我可以通过 Guzzle 向 Facebook 的 Graph API 发送 CURL 请求,以获取正在使用该应用程序的用户朋友。 But I can't seem to be able to obtain it.但我似乎无法获得它。 Here's my controller:这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use Socialite;
class SocialFriendsController extends Controller
{
public function getFacebookFriends()
{
$user = Socialite::driver('facebook')->user();
$access_token = $user->token;
$client = new Client();
$body = $client->get('https://graph.facebook.com/v2.11/me/friends', [
'query' => [
'access_token' => '$access_token',
]
])->getBody();
$result = \GuzzleHttp\json_decode($body, 1);
return view('pages.admin.posts.create', ['result' => $result]);
}
}
Once you have followed the previous answer to fix the reference to $access_token
you can do the following steps with the facebook SDK to retrieve a long lived token from Facebook so you can make many calls to the API. 一旦您按照上一个答案修复了对$access_token
的引用,您就可以使用facebook SDK执行以下步骤,从Facebook检索长期存在的令牌,以便您可以多次调用API。 The default auth token will work for 2 hours after which you will need to re-initiate the oAuth flow to get a new token. 默认身份验证令牌将工作2小时,之后您需要重新启动oAuth流以获取新令牌。 A long lived token gets you 2 months of access before having to do the oAuth again. 一个长期存在的令牌会让你获得2个月的访问权限,然后再次进行oAuth。 https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension
$facebook = new \Facebook\Facebook();
$client = $facebook->getOAuth2Client();
$twoMonthToken = $client->getLongLivedAccessToken($twoHourToken);
Update the user with the $twoMonthToken
使用$twoMonthToken
更新用户
A couple of things to note. 有几点需要注意。 First of all, the following part of your code won't work the way you expect it to: 首先,代码的以下部分将不会按照您期望的方式工作:
'access_token' => '$access_token',
You're trying to do variable interpolation, which doesn't work when you use single quotes ( '
). 您正在尝试进行变量插值,这在使用单引号( '
)时不起作用。 You should use double quotes ( "$access_token"
) instead, or since you're not actually doing anything else with that string, you could just remove the quotes altogether. 您应该使用双引号( "$access_token"
),或者因为您实际上没有对该字符串执行任何其他操作,所以您可以完全删除引号。
Secondly, when doing Facebook logins (In your case, using Socialite) the access token you receive from Facebook is single use (I think so anyway - at least from my own experience). 其次,在进行Facebook登录时(在您的情况下,使用Socialite),您从Facebook收到的访问令牌是一次性使用(我认为无论如何 - 至少从我自己的经验来看)。 When you do Socialite::driver('facebook')->user()
, Socialite is actually using the access token to grab the user object. 当您执行Socialite::driver('facebook')->user()
,Socialite实际上正在使用访问令牌来获取用户对象。 If you attempt to use it again, Facebook will return an error. 如果您再次尝试使用它,Facebook将返回错误。
Socialite is probably not the way to go to achieve what you're trying to do. 社交名流可能不是实现你想要做的事情的方式。 I would suggest using the Facebook SDK directly to get what you're after. 我建议直接使用Facebook SDK来获取你想要的东西。
$user = Socialite::driver('facebook')->userFromToken($access_token);
您可以像上面一样简单地传递您的 access_token ,它将返回用户信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.