2014-09-20 70 views
1

我有oauth訪問令牌,但我的get請求失敗。下面是我的代碼:如何使用PHP和cURL調用Gmail API?

$response = array(); 
    $crl = curl_init(); 
    $newurl = "https://www.googleapis.com/gmail/v1/users/[email protected]/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 
    } 

我證實,我的重點是正確的: Does my Google oAuth2 Token look right?

[email protected]肯定的是,提供的訪問之一。由於這是PHP,我無法調試,因爲我沒有控制檯(我試圖打印出它沒有打印的答覆)。

+0

它工作。我的應用程序類型是「Web應用程序」,所以我擁有的是一個ID和祕密。我沒有.pem文件。這是什麼,我在哪裏得到它? – user2316667 2014-09-20 20:13:57

回答

1

看來你沒有正確安裝ssl的證書。

我不建議停用SSL檢查,但你可以使用:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);. 

你應該做的事情是:

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

Downloaddirect)的證書,並使用上面的代碼。