Google+登录API问题 [英] Google+ login api problems

查看:119
本文介绍了Google+登录API问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中实现google + oauth2登录,而我目前遇到了一些问题.

我主要遵循的是此处

该示例似乎使用了许多类似twig的库,但我使用的是纯PHP,并且我认为该部分不会引起任何错误

我遇到的第一个问题是未找到 Google_HTTPRequest .我在某处读到一个由作曲家命名为 Google_HTTP_Request 的书,因此我更改了它,似乎已经解决了该问题.

然后php抱怨我在非静态函数 getIo()上进行了静态调用.所以我将 :: 更改为-> ,因为那对我来说很有意义.

然后我得到了错误

  PHP致命错误:调用未定义的方法Google_IO_Curl :: authenticatedRequest() 

可能与我所做的更改有关.

我正在运行的代码是

  $ google = new Google_Client();$ google-> setApplicationName('Hamster Weebly');$ google-> setClientId('CLIEnTID');$ google-> setClientSecret('himitsudesuyo');$ google-> setRedirectUri('postmessage');$ google-> authenticate($ _ POST ['AUTH_CODE']);$ token = json_decode($ google-> getAccessToken());//$ attrbutes = $ google-> verifyIdToken($ token-> id_token,'375022219321-us60lmg2cmeoj1aqpl784t7pbl1kg3jv.apps.googleusercontent.com')-> getAttributes();//error_log($ attrbutes ['payload'] ['sub']);$ reqUrl ='https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$ token-> access_token;$ req =新的Google_Http_Request($ reqUrl);$ tokenInfo = json_decode($ google-> getIo()-> authenticatedRequest($ req)-> getResponseBody());如果($ token->错误){http_response_code(500);出口;}//确保我们获得的令牌是针对目标用户的.if($ tokenInfo-> userid!= $ gPlusId){http_response_code(401);echo json_encode(令牌的用户ID与给定的用户ID不匹配");出口;}//确保我们获得的令牌用于我们的应用程序.如果($ tokenInfo->受众!= CLIENT_ID){http_response_code(401);echo json_encode(令牌的客户端ID与应用不匹配");出口;} 

我的问题是什么?

解决方案

在此行:

  $ tokenInfo = json_decode($ google-> getIo()-> authenticatedRequest($ req)-> getResponseBody()); 

由于(我认为)api内的迁移,应将

getIo()更改为 getAuth().

在那之后,我遇到了另一个更改了的错误:
if($ token->错误) if(isset($ tokenInfo-> error))

祝你好运!

I'm trying to implement google+ oauth2 login in my app and I am currently running into a few issues.

I am mainly following what is written here

It seems like the example is using a lot of libraries like twig, but I'm using maining pure php, and I don't think that part is causing any errors

The first issue I faced was that Google_HTTPRequest was not found. I read somewhere that composer names it Google_HTTP_Request, so I changed that and it seemed to have fixed that problem.

Then php was complaining that I was making a static call on a nonstatic function getIo(). So I changed to :: to -> because that was what made sense to me.

Then I am getting the error

PHP Fatal error:  Call to undefined method Google_IO_Curl::authenticatedRequest()

Which probably has something to do with what I changed.

The code I am running is

$google = new Google_Client();
$google->setApplicationName('Hamster Weebly');
$google->setClientId('CLIEnTID');
$google->setClientSecret('himitsudesuyo');
$google->setRedirectUri('postmessage');


$google->authenticate($_POST['AUTH_CODE']);
$token = json_decode($google->getAccessToken());

//$attrbutes = $google->verifyIdToken($token->id_token, '375022219321-us60lmg2cmeoj1aqpl784t7pbl1kg3jv.apps.googleusercontent.com')->getAttributes();

//error_log($attrbutes['payload']['sub']);

$reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' . $token->access_token;
$req = new Google_Http_Request($reqUrl);

$tokenInfo = json_decode($google->getIo()->authenticatedRequest($req)->getResponseBody());


if ($token->error)
{
    http_response_code(500);
    exit;
}

// Make sure the token we got is for the intended user.
if ($tokenInfo->userid != $gPlusId) {
    http_response_code(401);
    echo json_encode("Token's user ID doesn't match given user ID");
    exit;
}
// Make sure the token we got is for our app.
if ($tokenInfo->audience != CLIENT_ID) 
{
    http_response_code(401);
    echo json_encode("Token's client ID does not match app");
    exit;
}

What is my issue?

解决方案

In the line:

$tokenInfo = json_decode($google->getIo()->authenticatedRequest($req)->getResponseBody());

getIo() should be changed to getAuth() due to (I think) a migration within the api.

After that, I encountered another error in which I changed:
if ($token->error) to if ( isset($tokenInfo->error) )

Good Luck!

这篇关于Google+登录API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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