Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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 2.0不适用于服务器端web应用程序_Php_Google Api_Oauth 2.0 - Fatal编程技术网

Php OAuth 2.0不适用于服务器端web应用程序

Php OAuth 2.0不适用于服务器端web应用程序,php,google-api,oauth-2.0,Php,Google Api,Oauth 2.0,我正在尝试使用googlecontactsapi获取gmail联系人。为此,我使用了OAuth 2.0。为此,我在这里阅读了他们的OAuth 2.0使用指南。大体上,我遵循了这些步骤 向谷歌注册我的应用程序,获取客户id和客户机密,并向他们注册我的重定向uri 现在,我首先创建了一个名为sample.php的文件,如果用户点击getcontacts,他将被重定向到谷歌确认页面 现在,谷歌要求用户确认,如果用户同意提供其联系方式,那么他将被重定向到带有code的重定向uri 现在,我提取代码并发出

我正在尝试使用
googlecontactsapi
获取
gmail
联系人。为此,我使用了
OAuth 2.0
。为此,我在这里阅读了他们的OAuth 2.0使用指南。大体上,我遵循了这些步骤

  • 向谷歌注册我的应用程序,获取客户id和客户机密,并向他们注册我的
    重定向uri
  • 现在,我首先创建了一个名为
    sample.php
    的文件,如果用户点击
    getcontacts
    ,他将被重定向到谷歌确认页面
  • 现在,谷歌要求用户确认,如果用户同意提供其联系方式,那么他将被重定向到带有
    code
    重定向uri
  • 现在,我提取代码并发出post请求以获取OAuth令牌,如果我获得令牌,我将请求联系人
  • sample.php
    的代码如下所示

    <?php
    $client_id="my client id";
    $redirect_uri="http://localhost/htdocs/contacts/redirect.php";
    echo <<<doc
    <html>
    <body>
    <a href="http://accounts.google.com/o/oauth2/auth?client_id=$client_id&redirect_uri=$redirect_uri&scope=https://www.google.com/m8/feeds/&response_type=code">
    get contacts
    </a>
    doc;
    echo <<<doc
    </body>
    </html>
    doc;
    ?>
    
    <?php
    $client_id="my client id";
    $client_sec="my client secret ";  
    $redirect_uri="http://localhost/htdocs/contacts/redirect.php";
    $code=$_GET['code'];
    $post="code=$code&client_id=$client_id&client_secret=$client_sec&redirect_uri=$redirect_uri&grant_type=authorization_code";
    $post=urlencode($post);
     //the following curl request is made to get the authorization token
    $ch=curl_init();
    $url="https://accounts.google.com/o/oauth2/token";
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_AUTOREFERER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $json=curl_exec($ch);
    curl_close($ch);
    echo $json;     // this is showing Required parameter is missing: grant_type Error 400
    $res=json_decode($json,true);
    print_r($res);   // this is just for debugging
    $token=$res['access_token'];
    echo $token;   // this is just for debugging
    $url="https://www.google.com/m8/feeds/contacts/default/full?oauth_token=$token";
      //the following curl request is made to get the contacts
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_AUTOREFERER,1);
    $xml=curl_exec($ch);
    echo $xml;
    curl_close($ch);
    $dom= new DOMDocument();
    $dom->loadXML($xml);
    $xpath=new DOMXPath($dom);
    $path='//gd:email';
    $nodes=$xpath->query($path);
    echo $nodes->length."<br />";
    foreach($nodes as $node)
    {
    $email=$node->attributes->getNamedItem('address')->nodeValue;
    echo $email."<br />";
    }
    ?>
    
    现在的问题是步骤4失败了。直到确认步骤,当我点击
    Allow access
    时,我被重定向到我的
    redirect uri
    ,但是它说

    缺少必需参数:授予类型错误400

    现在我不明白这一点,因为我提供了
    grant\u type
    参数。我查看了
    firebug
    ,发现正在发出post请求,但不是我想要的。正在发出的post请求是这样的
    https://accounts.google.com/o/oauth2/approval?xsrfsign=AC9jObYAAAAATkfm3uvx0sfsW7WVmB_FeRqVPLjDcTLz
    状态显示
    302已临时移动
    。我不明白发生了什么事。他们是否更改了OAuth工作流中的某些内容?需要帮忙吗


    编辑 正如我建议的那样,我删除了grant_type之前的空格,它起了作用。现在我可以获得访问令牌,但仍然无法获得联系人。(我在第35行的htdocs\contacts\redirect.php中得到了一个错误,因为
    空字符串作为输入提供给htdocs\contacts\redirect.php,而
    第35行的
    引用了
    $dom->loadXML($xml)
    ;因此似乎没有发出get请求)此外,我无法看到在
    firebug
    中发出post请求(但肯定是因为我得到了访问令牌作为响应)。我也没有在
    firebug
    中看到我在post请求后发出的get请求有什么问题


    更新:
    问题是请求是在https上的,我没有为https使用适当的头。我使用了正确的标题,效果很好。问题解决了吗?大概因为我仍然不明白为什么我无法在
    firebug's'
    net`选项卡中看到那些get和post请求

    您正在将一个已经URL编码的字符串传递给PHP:


    这会弄乱值,谷歌的服务器无法正确解析它,因此会报告丢失的参数。删除
    $post=urlencode($post)行,看看它是否工作得更好。

    &
    授权类型之间有一个空格。这可能会导致谷歌无法识别该参数。

    “因为我仍然不明白为什么我无法在firebug的“net”选项卡中看到这些get和post请求。”


    我想,既然您是在请求使用curl,那么它与firefox无关,因为curl是一个服务器端库,因此curl发布的帖子不会通过浏览器,firebug也看不到它们。

    可能是打字错误,但在&和grant_类型之间有一个空格。@Ikke感谢您的帮助。现在我可以获得
    访问令牌
    ,但还不能获得联系人。让我看看是否也有联系人。将其作为答案发布,我会将其标记为已接受。再次感谢“无法获取联系人”是什么意思?@ike我的意思是我没有获取联系人页面。我没有看到对联系人的curl-get请求,我在第35行的C:\xampp\htdocs\contacts\redirect.php中得到了一条消息作为输入提供的
    空字符串谢谢。请看我问题中的编辑。我对
    firebug
    $post="code=$code&client_id=$client_id&client_secret=$client_sec&redirect_uri=$redirect_uri&grant_type=authorization_code";
    $post=urlencode($post);