facebook - facebook更改后获取facebook用户头像(10月24日)

标签 facebook asp.net-core facebook-graph-api .net-core

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

所以我和 SOF 从 facebook 中获取用户的个人资料图片,如下所示: https://graph.facebook.com/1108834579148558/picture?type=large

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

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

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

最佳答案

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

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 文档 here 上找到有关 access_token 的更多信息.
App Access Token
Client Access Token

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

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

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

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 是否长期存在。我现在使用同一个 URL 将近一个星期,所以我认为一旦您使用有效的 access_token 请求它,该 URL 就会保持原样。

PHP 请求示例

因为我已经在 PHP 中实现了它,所以我无法为您提供示例代码,但可以向您展示我在 Laravel 框架中使用 Guzzle 客户端完成它的方式。您可以查看我为 Laravel Socialite 升级的 Provider Class 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);
    }

构建自己的访问 token 的快速而肮脏的方法(不推荐)

有一种方法可以构建您自己的 access_token 但这应该仅用于测试目的,因为您提供了完整的凭据并且可能被劫持。您可以通过使用您的凭据和 | 作为此格式的分隔符构建一个串联字符串来实现这一点:{cliend_id}|{client_secret}

关于facebook - facebook更改后获取facebook用户头像(10月24日),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64434913/

相关文章:

Facebook Like Box 插件不会为注销的用户显示;仅适用于登录用户

ios - Facebook 登录按钮对齐 iOS

asp.net-core - 暴露应用程序命令的强类型 ID?

托管在 SSL 上的 Facebook 应用程序为某些访问者提供 "Connection not trusted"错误

python - 与基于网络的游戏互动

c# - ASP.net 核心 MVC 捕获所有路由服务静态文件

c# - net::ERR_NAME_NOT_RESOLVED docker

ios - 如何使用自定义 url 直接启动到 Facebook iOS 应用程序中的帖子评论页面?

facebook - 使用 graph api 私下发布到 facebook 上的 friend 墙上

ruby-on-rails - 我应该如何将 Facebook 调用数据缓存到我的数据库中,以便 Rails 可以在本地使用它?