通过 Google 登录:获得访问令牌后要做什么?

问题描述

我正在学习如何在我的 Joomla 网站上通过 Google 按钮创建登录,并且我正在按照 https://developers.google.com/identity/protocols/oauth2/web-server 上的说明进行操作。

一点背景: 我正在使用第三方扩展程序来处理社交登录。它的 facebook 登录运行良好,但它的 google 登录已过时,仍在尝试连接到 Google Plus 端点。点击我页面上的登录按钮确实会进入Google的帐户选择屏幕,在我选择一个帐户并授予权限后,回调页面上有一个简单的错误消息。作者已经停止更新扩展,为了学习,我决定自己修复。

我取得的成就: 目前,我能够从 Google 获取访问令牌。

.......
$postdata = array(
                'grant_type'    =>'authorization_code','client_id'     => $this->params->get('goappid'),'client_secret' => $this->params->get('gosecret'),'redirect_uri'  => $this->getRedirectURL().'&task=Gologin','code'          => $_GET['code']);

            curl_setopt($curl,CURLOPT_URL,'https://oauth2.googleapis.com/token');
            curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
            curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($postdata));

            $oauth = json_decode(curl_exec($curl));

        //  Above code seems to be fine,  getting $oauth response as follows
        //   "access_token": "token string",//   "expires_in": 3599,//   "scope": "https://www.googleapis.com/auth/userinfo.profile",//   "token_type": "Bearer",//  "id_token":"token string" 

            if (isset($oauth->access_token)) {
                curl_setopt($curl,CURLOPT_POST,false);
                curl_setopt($curl,'https://www.googleapis.com/plus/v1/people/me?access_token='.$oauth->access_token);  //Apparently outdated endpoint

                $user = json_decode(curl_exec($curl));

                if (empty($user->error)) {
                    curl_setopt($curl,'https://www.googleapis.com/plus/v1/people/me/activities/public?access_token='.$oauth->access_token);//Apparently outdated endpoint

我的问题:此时,我不知道该怎么办。 instruction 表示 After your application obtains an access token,you can use the token to make calls to a Google Api on behalf of a given user account,但我如何“调用 Google Api”?要通过 Google 按钮进行简单登录,我应该调用哪个 API?我应该向哪个端点发出请求?我无法从说明页面找到此信息。上面的代码正在向 https://www.googleapis.com/plus/v1/people/me?access_token 发出请求,这显然已经过时了,但我应该如何更改它?这应该由说明提供,但我找不到它。如果我想访问其他 Google Api,我该如何“调用”它们?也就是我在哪里可以找到每个 API 的端点?

我还阅读了 https://developers.google.com/identity/protocols/oauth2/openid-connect,我正在尝试做的事情是否被视为 OIDC?我应该按照这个文件进行吗?

解决方法

我仍然认为查询路径中的 access_token 在某些 Google api 中有效。直到June 2021

GET https://www.googleapis.com/plus/v1/people/me?access_token=[token]

我建议改用授权标头

GET https://people.googleapis.com/v1/%5BRESOURCENAME%5D HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json