繁体   English   中英

如何使用PHP和cURL调用Gmail API?

[英]How to call Gmail API using PHP and cURL?

我有oauth访问令牌,但我的get请求失败。 下面是我的代码:

    $response = array();
    $crl = curl_init();
    $newurl = "https://www.googleapis.com/gmail/v1/users/myemail@gmail.com/messages?access_token=" . $result['access_token'];
    echo $newurl . "</br>";
    curl_setopt($crl, CURLOPT_HTTPGET, true);
    curl_setopt($crl, CURLOPT_URL, $newurl);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    $reply = curl_exec($crl);
    if ( $reply ) {
        echo 'successfully retrieved messages</br>';
        echo $reply;
    } else {
        echo 'failed to get messages</br>'; // prints
        echo $reply; // prints nothing
    }

我确认我的密钥正确: 我的Google oAuth2令牌看起来正确吗?

myemail@gmail.com当然是提供访问权限的站点。 由于这是PHP,因此无法调试,因为我没有控制台(我试图打印出不打印任何内容的回复)。

似乎您没有正确安装ssl的证书。

我不建议禁用ssl检查,但是您可以使用:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);.

您应该做的是:

curl_setopt($ch, CURLOPT_CAINFO, "path/to/cacert.pem");

下载直接 )证书并使用上面的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM