所以facebook改变了网站获取用户配置文件图像的方式,所有细节都在这里:https://developers.facebook.com/docs/graph-api/reference/user/picture/
因此,我和SOF从facebook获取用户的配置文件图像如下:https://graph.facebook.com/1108834579148558/picture?type=large
现在我们得到了facebook提供的默认图像。
在他们编写的facebook文档中,我们需要附加访问令牌,以便从现在开始获取用户配置文件映像,它是如何工作的?
我唯一能想到的是在用户登录时,facebook可以检索用户配置文件图像,任何人都可以帮助这一点,我正在使用.Net核心。
发布于 2020-10-23 22:03:28
Facebook解释了2020年10月后在新文档这里中获取用户图像的新方法,并说明了所需的更改如下:
此端点支持应用程序范围内的用户ID (ASID)、用户ID (UID)和页面范围内的用户ID (PSID)。目前,您可以在不需要的情况下查询ASID和UID。然而,从2020年10月24日开始,所有基于UID的查询都需要一个访问令牌.如果查询一个UID,因此必须包括一个令牌:
1.获取facebook提供的任何形式的access_token
根据您的应用程序结构,您可以使用Facebook提供的下列任何一种方法来获得access_token。您可以在FB文档access_token上找到更多关于这里的信息。
作为客户端访问令牌的一个示例,我正在使用:
使用以下参数向https://graph.facebook.com/oauth/access_token发出GET请求:
[
"client_id" => #FACEBOOK_CLIENT_ID,
"client_secret" => #FACEBOOK_CLIENT_SECRET,
"grant_type" => "client_credentials",
]作为响应,您将得到您的access_token,您需要附加到新的图像URL。
2.获取带有access_token的完整的化身URL。
截至2020年10月23日,这对我来说是可行的。现在是完整的图像格式
{$this->graphUrl}/{$this->version}/{$userID}/picture?type=large&redirect=false&access_token={$access_token}
如果您设置了redirect=false,您将得到一个JSON对象作为响应:
{
"data": {
"height": 100,
"is_silhouette": false,
"url": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10152700727498624&height=100&width=100&ext=1606081666&hash=AeTQyGgugiSbRcB7Sxw",
"width": 100
}
}另一方面,如果您离开了redirect=true,或者根本没有设置它,您将得到图像本身,然后您可以将其保存在磁盘上,或者使用url作为图像路径。但我不确定给定的URL是否长寿。我得到了相同的网址工作了几乎一个星期,所以我认为该网址将保持原样,一旦你要求它与一个有效的access_token。
PHP中的示例请求
正如我在PHP中实现的那样,我不能给出一个示例代码,而是向您展示我是如何在Laravel中使用give客户端的。你可以看看我的升级供应商级的拉勒维尔社会名流这里。
public function getAccessToken(){
// Make a request to get the Access Client
$res = Http::get("https://graph.facebook.com/oauth/access_token", [
"client_id" => config('services.facebook.client_id'),
"client_secret" => config('services.facebook.client_secret'),
"grant_type" => "client_credentials",
]);
// Response is a JSON Object, so decode it into an Array
$r = $res->json();
// Return the access_token Array Key out of the response
return Arr::get($r, 'access_token', false);
}
public function getFacebookAvatar(array $user){
// get the access_token from the above method
$access_token = $this->getAccessToken();
// build the new URI path for the User Image
$path = "{$this->graphUrl}/{$this->version}/{$userID}/picture?type=large&redirect=false&access_token={$access_token}";
$res = Http::get($path);
// Get the final User Image URL out of the response
return Arr::get($res->json(), 'data.url', false);
}快速和肮脏的方法来构建您自己的访问令牌(不推荐)
有一种方法可以构建您自己的access_token,但是应该只用于测试目的,因为您提供了完整的凭据,并且可能会被劫持。您可以通过构建一个连接的字符串和您的凭据以及作为分隔符的| (格式为:{cliend_id}|{client_secret} )来归档。
https://stackoverflow.com/questions/64434913
复制相似问题