php - 通过 PHP 使用 Google 身份验证 API

标签 php google-api

以下代码不是打印用户信息,而是打印 Google 授予的访问 token 的详细信息。代码很简单,但我无法找出具体原因

    <?php
$url = 'https://accounts.google.com/o/oauth2/token';
$fields = array(
        'code'=>urlencode($_GET['code']),
        'client_id'=>urlencode('###########.apps.googleusercontent.com'),
        'client_secret'=>urlencode('###########'),
        'redirect_uri'=>urlencode('http://######odie.co.in/googlogin'),
        'grant_type'=>urlencode('authorization_code'),
                    );


foreach($fields as $key=>$value) {
     $fields_string .= $key.'='.$value.'&';
     }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);


//close connection
curl_close($ch);
$data =  json_decode($result);

print_r(file_get_contents(' https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$data['access_token']));
?>

最佳答案

您需要将CURLOPT_RETURNTRANSFER, true 添加到您的curl_setopt 中。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

关于php - 通过 PHP 使用 Google 身份验证 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9125181/

相关文章:

c# - 在 Google URL Shortener API 调用中获取 System.Net.WebException : The remote server returned an error: (403) Forbidden.

swift - Google API - 凭据无效

php - gettext 中所有语言的复数形式列表?

php - 使用高级php上传图片

php - 如何匹配PHP数组中的值然后找到键值?

javascript - Ajax 到 Laravel 返回 404 并且 Responsejson 未定义

coldfusion - 如何将 Google 文档列表数据 API 与 Coldfusion 页面集成?

Django 。 Google 地点自动填写地址表单

php - 调用未定义的方法 UserModel::addUser()

Android - Google Maps API v2 - SupportMapFragment 错误