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

查看:32
本文介绍了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

现在我们得到了 facebook 提供的默认图片.

在他们编写的 Facebook 文档中,我们需要附加访问令牌才能从现在开始获取用户个人资料图片,它是如何工作的?

我唯一能想到的是在用户登录时,facebook 可以检索用户个人资料图片,有没有人可以提供帮助,我正在使用 .Net 核心.

解决方案

Facebook 在其新文档中解释了 2020 年 10 月之后获取用户图像的新方法 here 并且他们声明所需的更改如下:

<块引用>

此端点支持应用范围用户 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 对象作为响应:

<代码>{数据":{高度":100,is_silhouette":假,"url":"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10152700727498624&height=100&width=100&ext=1606081666&hash=AeTbxRgi"Sgi"SgiGgu;S宽度":100}}

另一方面,如果您离开 redirect=true 或根本不设置它,您将获得图像本身,然后您可以将其保存在磁盘上或使用 url 作为图像路径.但我不确定给定的 URL 是否长期存在.我让相同的 URL 工作了将近一个星期,所以我认为一旦您使用有效的 access_token 请求该 URL,该 URL 将保持原样.

PHP 请求示例

因为我已经在 PHP 中实现了它,所以我不能给你一个示例代码,但会向你展示我在 Laravel 框架中使用 Guzzle 客户端完成它的方式.您可以在此处查看我升级后的 Laravel Socialite Provider 类.

 公共函数 getAccessToken(){//请求获取访问客户端$res = Http::get("https://graph.facebook.com/oauth/access_token", [client_id"=>配置('services.facebook.client_id'),client_secret"=>配置('services.facebook.client_secret'),grant_type"=>client_credentials",]);//响应是一个 JSON 对象,因此将其解码为一个数组$r = $res->json();//从响应中返回 access_token Array Key返回 Arr::get($r, 'access_token', false);}公共函数 getFacebookAvatar(array $user){//从上面的方法中获取access_token$access_token = $this->getAccessToken();//为用户图像构建新的 URI 路径$path = "{$this->graphUrl}/{$this->version}/{$userID}/picture?type=large&redirect=false&access_token={$access_token}";$res = Http::get($path);//从响应中获取最终的用户图像 URL返回 Arr::get($res->json(), 'data.url', false);}

建立自己的访问令牌的快速而肮脏的方式(不推荐)

有一种方法可以构建您自己的 access_token,但这应该仅用于测试目的,因为您提供的完整凭据可能会被劫持.您可以通过使用您的凭据和 | 作为以下格式的分隔符构建一个连接字符串来存档:{cliend_id}|{client_secret}.

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

Now we get the default image facebook provides.

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?

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

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

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",
]

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

2. Get the full avatar URL with the access_token attached.

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}

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

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.

Example Request in PHP

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);
    }

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天全站免登陆