Facebook更改后获取Facebook用户个人资料图片(10月24日) [英] Get facebook user profile image after facebook changes(October 24)

查看:173
本文介绍了Facebook更改后获取Facebook用户个人资料图片(10月24日)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,facebook更改了网站获取用户个人资料图像的方式,所有详细信息都在此处: https://developers.facebook.com/docs/graph-api /reference/user/picture/

因此,我以及 SOF 都从Facebook获得了用户的个人资料图片,如下所示: https://graph.facebook.com/1108834579148558/picture?type=large

So facebook changed the way websites get the user profile image, all the details are here: https://developers.facebook.com/docs/graph-api/reference/user/picture/

So me and also SOF get the profile image of a user from facebook like this: https://graph.facebook.com/1108834579148558/picture?type=large

现在,我们获得了Facebook提供的默认图像.

Now we get the default image facebook provides.

在facebook文档中,他们写道,我们需要附加访问令牌才能从现在开始获取用户个人资料图片,

In facebook docs they write we need to attach Access Token in order to get the user profile image from now, How does it work?

我唯一能想到的是,在用户登录时,facebook可以检索用户个人资料图像,任何人都可以提供帮助,我使用的是.Net core.

The only thing I can think of is upon user login facebook can retrieve the user profile image, does anyone can help with this, I am using .Net core.

推荐答案

Facebook在其新文档

Facebook explains the new way of getting the User Image after October 2020 in their new docs here and they state the required changes as following:

此端点支持应用程序范围的用户ID(ASID),用户ID(UID)和页面范围的用户ID(PSID).当前,您可以查询ASID和UID,而无需任何要求.但是,从2020年10月24日开始,所有基于UID的查询都将需要访问令牌.如果您查询一个UID,因此必须包含一个令牌:

This endpoint supports App-Scoped User IDs (ASID), User IDs (UID), and Page-Scoped User IDs (PSID). Currently you can query ASIDs and UIDs with no requirements. However, beginning October 24, 2020, an access token will be required for all UID-based queries. If you query a UID and thus must include a token:

1.以Facebook为您提供的任何形式获取access_token

根据您的应用程序结构,您将使用Facebook提供的以下方法之一来获取access_token.您可以在FB文档的此处上找到有关access_token的更多信息.
应用访问令牌
客户端访问令牌

1. Get an access_token in any form that facebook provides you

Depending on your application structure you would use either one of the following methods provided by Facebook to gain an access_token. You can find more about access_token on the FB docs here.
App Access Token
Client Access Token

作为我正在使用的客户端访问令牌的示例:
使用以下参数向https://graph.facebook.com/oauth/access_token发出GET请求:

As an Example for the Client Access Token which I am using:
Make a GET Request to https://graph.facebook.com/oauth/access_token with the following parameters:

[
   "client_id"     => #FACEBOOK_CLIENT_ID, 
   "client_secret" => #FACEBOOK_CLIENT_SECRET, 
   "grant_type"    => "client_credentials",
]

作为响应,您将得到您的access_token,您需要将其附加到新的图像URL上.

As Response you will get your access_token which you need to attach on the new Image URL.

截至2020年10月23日,这对我有用. 现在,完整图片格式为
{$this->graphUrl}/{$this->version}/{$userID}/picture?type=large&redirect=false&access_token={$access_token}

As of 2020 October 23 this is working for me. The Full Image format is now
{$this->graphUrl}/{$this->version}/{$userID}/picture?type=large&redirect=false&access_token={$access_token}

如果设置redirect=false,您将获得一个JSON对象作为响应:

If you set redirect=false you will get a JSON Object as Response:

{
    "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是否长期存在.我的同一个URL可以使用将近一个星期,因此,我认为只要您使用有效的access_token进行请求,该URL就会保持不变.

In the other hand if you leave redirect=true or doesn't set it at all you will get the Image itself which you can then save on your disk or use the url as the image path. But I am not sure if that given URL is long lived or not. I got the same URL working for almost one Week now, so I think that URL will stay as it is once you have requested for it with an valid access_token.

正如我在PHP中实现的那样,我无法为您提供示例代码,而是向您展示我如何使用Laravel Framework中的Guzzle Client完成此操作.您可以在此处查看升级后的Laravel Socialite Provider类.

As I have implemented that in PHP I cannot give you an example code but show you the way I've done it with the Guzzle Client within the Laravel Framework. You can have a look into my upgraded Provider Class for Laravel Socialite here.

    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}.

Quick and dirty way to build your own Access Token (not recommended)

There is one way to build your own access_token but this should be used only for testing purposes due the fact you are providing your full credentials and that could be hijacked. You can archieve that by building a concatened string with your credentials and the | as delimiter in this format: {cliend_id}|{client_secret}.

这篇关于Facebook更改后获取Facebook用户个人资料图片(10月24日)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆