前端是100%JS。用户点击登录按钮,收到authResult['code']
并通过ajax发送到localhost/api/user/login
,其中包含以下内容:
$code = $data['code'];
require_once 'Google/Client.php';
$client = new Google_Client();
$client->setClientId('xxxxxx');
$client->setClientSecret('xxxxx');
$client->setRedirectUri('http://localhost:8080');
$client->setScopes('email'); //Why do I need this? I already set scope in JS.
$client->authenticate($code); //It fails here. with no error. just 400 bad request.
$token = json_decode($client->getAccessToken());
$reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' .
$token->access_token;
$req = new Google_HttpRequest($reqUrl);
$tokenInfo = json_decode(
$client::getIo()->authenticatedRequest($req)->getResponseBody());
//Check errors.
//Save user personal info in database
//Set login sessions
答案 0 :(得分:0)
您还可以尝试手动构建网址并使用curl调用它,以确保所有内容符合您的预期:https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse