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

问题描述:

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

I have the oauth access token but my get request fails. Below is my code:

    $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令牌是否正确?

I confirmed that my key is correct: Does my Google oAuth2 Token look right?

myemail@gmail.com当然是提供访问的。因为这是PHP,我不能调试,因为我没有一个控制台(我试图打印出来的答复它什么也没打印)。

myemail@gmail.com is certainly the one that provided access. Since this is PHP, I cannot debug because I do not have a console (I tried to print out the reply it prints nothing).

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

It seems that you do not have the certificates for ssl installed properly.

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

I do not recommend disabling ssl check, but you can use:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);.

你应该做的事情是:

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

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

Download (direct) the cert and use the above code.