0

我正在使用 SOAP 网络服务来获取 gmail 收件箱。但是,它要求我为各自的 API 设置 Google Key。

如何为 wsdl 设置谷歌密钥?有任何想法吗?

我正在连接到 gmail API,但是我无法将响应放入我的页面。

代码:-

"<div>
<a href='https://accounts.google.com/o/oauth2/auth?client_id=954719584955.apps.googleusercontent.com&redirect_uri=https://localhost/gmail&scope=https://mail.google.com/mail/feed/atom/&response_type=code'> Retrieve your Google Mails using webservices.</a>
</div>"

它重定向到我已注册的页面,但没有获得我请求的谷歌内容。

代码:-

    $client_id='954719584955.apps.googleusercontent.com';
    $client_secret='vyY-DRInNsofJL2MTmpaVRYP';
    $redirect_uri='https://localhost/gmail';
    $max_results = 200;

    $auth_code = $_GET["code"];

    function curl_file_get_contents($url)
    {
     $curl = curl_init();
     $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

     curl_setopt($curl,CURLOPT_URL,$url);   //The URL to fetch. This can also be set when initializing a session with curl_init().
     curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);    //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
     curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);   //The number of seconds to wait while trying to connect.    

     curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);  //To follow any "Location: " header that the server sends as part of the HTTP header.
     curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
     curl_setopt($curl, CURLOPT_TIMEOUT, 10);   //The maximum number of seconds to allow cURL functions to execute.
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate.

     $contents = curl_exec($curl);
     curl_close($curl);
     return $contents;
    }


$fields=array(
    'code'=>  urlencode($auth_code),
    'client_id'=>  urlencode($client_id),
    'client_secret'=>  urlencode($client_secret),
    'redirect_uri'=>  urlencode($redirect_uri),
    'grant_type'=>  urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($curl);
curl_close($curl);

$response =  json_decode($result);
$accesstoken = $response->access_token;

$url = 'https://mail.google.com/mail/feed/atom/??max-results='.$max_results.'&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) //At times you get Authorization error from Google.
{
    echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
    exit();
}

这是 gmail API 的完整代码。

请帮帮我。谢谢。

4

1 回答 1

1

您应该访问google apis 控制台。在那里您可以注册一个应用程序并设置您将要使用的 API。

于 2012-07-20T14:21:43.000 回答