GoogleOAuthJavaScript到PHP-如何获取刷新和访问令牌
我在这里学习这个教程 我已经从javascript获得了代码GoogleOAuthJavaScript到PHP-如何获取刷新和访问令牌,php,redirect,google-api,Php,Redirect,Google Api,我在这里学习这个教程 我已经从javascript获得了代码 auth2.grantOfflineAccess({ scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.business.manage' }).then(signInCallback); fun
auth2.grantOfflineAccess({
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.business.manage'
}).then(signInCallback);
function signInCallback(resp) {
var auth_code = resp.code;
console.log("AuthCode:" + auth_code)
// Obviously here I want to submit the code to my PHP server to exchange it for a refresh and access token
}
现在我有了response.code。显然,我必须将其发送到PHP以获取访问令牌作为回报。但是PHP似乎需要一个重定向URI??或者如果我用“postmessage”,显然不是
但无论如何,每当我向服务器提交代码以尝试获取访问令牌时。它失败了
invalid grant
这是我的php代码
$client = new \Google\Client();
$client->setAuthConfig(storage_path('/app/credentials.json'));
$client->setAccessType("offline");
$client->setIncludeGrantedScopes(true);
$client->addScope(
array(
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/plus.business.manage'
)
);
$token= $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token在此返回
Array
(
[error] => invalid_grant
[error_description] => Bad Request
)
从我一直在读的内容来看,我必须使用重定向协议(但我真的不想这样做,因为这是一个用Vue.js编写的SPA应用程序,我更不想做重定向。我想将代码发送到服务器,获取访问令牌,存储它,并在服务器端用于将来的请求
我该怎么做
根据我读的那页,有可能
但是,它们只包括Java和Python示例???您混淆了哪些参数需要转到哪里: 在授权请求(您将用户重定向到的URL,在javascript中)中,您需要指定:
范围
包括授予的范围
access\u type=offline
- 重定向URI
- 您的客户ID
- 重定向URI
- 授权码
- 您的客户ID和客户机密
重定向\u uri
(文档说,如果您没有应用程序的web版本,您可以指定一个空字符串)。也可能是您的客户端ID不匹配
就您的具体情况而言,可能有必要研究能够为您做到这一点的托管提供商,特别是如果您正在建设一个SPA,我所在的SPA旨在与SPA合作,这样您就可以纯粹在前端将用户连接到Google,然后获得(刷新)无论何时需要,都可以在后端访问令牌。谢谢。不过我已经解决了。我只需要在javascript中指定“postmessage”作为URI重定向。在后端也是如此。这迫使它不使用重定向协议