Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌oauth认证_Php_Oauth_Google Api - Fatal编程技术网

Php 谷歌oauth认证

Php 谷歌oauth认证,php,oauth,google-api,Php,Oauth,Google Api,我试图通过授权,然后从google plus获取数据,但数据变量始终显示“暂时移动”的值。我做错了什么 <?php session_start(); $client_id = ''; $client_secret = ''; $api_key = ''; $redirect_uri = 'http://localhost:8888/oauth'; $scope = "https://www.googleapis.com/auth/plus.me";

我试图通过授权,然后从google plus获取数据,但数据变量始终显示“暂时移动”的值。我做错了什么

<?php
session_start();

    $client_id = '';
    $client_secret = '';
    $api_key = '';
    $redirect_uri = 'http://localhost:8888/oauth';
    $scope = "https://www.googleapis.com/auth/plus.me";

    if (!isset($_REQUEST['code']))
    {
        $login_path = "https://accounts.google.com/o/oauth2/auth?";
        $login_path .= "redirect_uri=" . $redirect_uri;
        $login_path .= "&response_type=code";
        $login_path .= "&client_id=" . $client_id;
        $login_path .= "&approval_prompt=force";
        $login_path .= "&scope=" . $scope;
        $login_path .= "&access_type=offline";

        echo "<a href='" . $login_path . "'>login</a>";
    }

    else
    {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/auth");

        curl_setopt($ch, CURLOPT_POST, TRUE);

        // This option is set to TRUE so that the response
        // doesnot get printed and is stored directly in
        // the variable
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $post_params = "code=" . $_REQUEST['code'] . "&";
        $post_params = "redirect_uri=" . $redirect_uri . "&";
        $post_params .= "client_id=" . $client_id . "&";
        $post_params .= "scope=" . $scope . "&";
        $post_params .= "client_secret=" . $client_secret . "&";
        $post_params .= "grant_type=authorization_code&";
        $post_params .= "response_type=code";


        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);

        $data = curl_exec($ch);

        curl_close($ch);

        //commented this since data was showing empty
        //$data = json_decode($data);

        print '<pre>';
        print $data;
        print '</pre>';
}

更新:对于其他有同样问题的人,我已经解决了。这是我的工作代码

$client_id='';
$client_secret='';
$api_key='';
$redirect_uri='1http://localhost:8888/oauth';
$scope=”https://www.googleapis.com/auth/plus.me";
$api_调用=”https://www.googleapis.com/plus/v1/people/me?access_token=";
如果(!isset($_请求['code']))
{
$login_path=”https://accounts.google.com/o/oauth2/auth?";
$login\u path.=“redirect\u uri=”.$redirect\u uri;
$login\u path.=“&response\u type=code”;
$login\u path.=“&client\u id=”.$client\u id;
$login\u path.=“&approval\u prompt=force”;
$login_path.=“&scope=”.$scope;
$login\u path.=“&access\u type=offline”;
回声“;
}
其他的
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,”https://accounts.google.com/o/oauth2/token");
curl_setopt($ch,CURLOPT_POST,TRUE);
//此选项设置为TRUE,以便响应
//不打印,直接存储在
//变量
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$post_params=“code=”..$\u请求['code']”和;
$post_params.=“重定向_uri=”.$redirect_uri.&”;
$post_参数=“客户机id=”.$client_id.&”;
$post_params.=“scope=”.$scope.&”;
$post_params.=“客户机_secret=”.$client_secret.&”;
$post_参数=“授权类型=授权代码&”;
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_params);
$data=curl\u exec($ch);
卷曲关闭($ch);
$data=json_decode($data);
$access\u token=$data->access\u token;
$refresh\u token=$data->refresh\u token;
//oauth的末尾现在调用google plus api。not$api\u调用保存请求uri值
$call=$api\u call.$access\u令牌;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$call);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$out=curl\u exec($ch);
回声';
打印(输出);
回声';

}

别忘了将编辑内容转换为答案,并在系统允许时将其标记为已接受。谢谢我这么做,我想我必须等待2天才能接受。
$client_id = '';
$client_secret = '';
$api_key = '';
$redirect_uri = 'http://localhost:8888/oauth';
$scope = "https://www.googleapis.com/auth/plus.me";
$api_call = "https://www.googleapis.com/plus/v1/people/me?access_token=";

if (!isset($_REQUEST['code']))
{
    $login_path = "https://accounts.google.com/o/oauth2/auth?";
    $login_path .= "redirect_uri=" . $redirect_uri;
    $login_path .= "&response_type=code";
    $login_path .= "&client_id=" . $client_id;
    $login_path .= "&approval_prompt=force";
    $login_path .= "&scope=" . $scope;
    $login_path .= "&access_type=offline";

    echo "<a href='" . $login_path . "'>login</a>";
}

else
{

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");

    curl_setopt($ch, CURLOPT_POST, TRUE);

    // This option is set to TRUE so that the response
    // doesnot get printed and is stored directly in
    // the variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


    $post_params = "code=" . $_REQUEST['code'] . "&";
    $post_params .= "redirect_uri=" . $redirect_uri . "&";
    $post_params .= "client_id=" . $client_id . "&";
    $post_params .= "scope=" . $scope . "&";
    $post_params .= "client_secret=" . $client_secret . "&";
    $post_params .= "grant_type=authorization_code&";

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);

    $data = curl_exec($ch);

    curl_close($ch);

    $data = json_decode($data);

    $access_token = $data->access_token;
    $refresh_token = $data->refresh_token;

    // end of oauth now call google plus api. not $api_call holds the request uri value
    $call = $api_call . $access_token;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $call);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $out = curl_exec($ch);

    echo '<pre>';
    print_r($out);
    echo '</pre>';