2014-02-21 78 views
0

我正在嘗試構建「儘可能簡單」的例程來檢索AdWords廣告系列名稱。AdWords API過期Cookie錯誤

不過,我收到此錯誤信息

AuthenticationError.LOGIN_COOKIE_REQUIRED @;觸發器:'<null>'

每當我第一次運行下面的PHP腳本。

後續運行正常工作幾個小時,直到我再次得到錯誤(我相信所引用的cookie再次到期)。

這個cookie是關於什麼的?有人可以指出發生了什麼以及如何解決它?

<?php 

$refreshToken=/* omitted */; 
$clientId=/* omitted */; 
$clientSecret=/* omitted */; 
$clientCustomerId=/* omitted */; 
$developerToken=/* omitted */; 

$ch=curl_init('https://accounts.google.com/o/oauth2/token'); 
curl_setopt($ch,CURLOPT_HEADER,false); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST'); 
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query(array(
    'refresh_token'=>$refreshToken, 
    'client_id'=>$clientId, 
    'client_secret'=>$clientSecret, 
    'grant_type'=>'refresh_token', 
))); 
$json=curl_exec($ch); 
curl_close($ch); 
$object=json_decode($json); 
$accessToken=$object->access_token; 

$namespace='https://adwords.google.com/api/adwords/cm/v201309'; 
$soapClient=new SoapClient($namespace.'/CampaignService?wsdl',array(
    'features'=>SOAP_SINGLE_ELEMENT_ARRAYS, 
    'encoding'=>'utf-8', 
    'stream_context'=>stream_context_create(array(
     'http'=>array(
      'header'=>'Authorization : Bearer '.$accessToken, 
     ), 
    )), 
)); 
$soapClient->__setSoapHeaders(new SoapHeader($namespace,'RequestHeader',array(
    'clientCustomerId'=>$clientCustomerId, 
    'developerToken'=>$developerToken, 
    'userAgent'=>'TestApp', 
    'validateOnly'=>false, 
    'partialFailure'=>false, 
))); 
try 
{ 
    $result=$soapClient->get(array(
     'serviceSelector'=>array(
      'fields'=>array('Name'), 
     ), 
    )); 
} 
catch (SoapFault $e) 
{ 
    $result=$e->getMessage(); 
} 
var_dump($result); 
+0

我懷疑這是關係到WSDL緩存:如果我設置「cache_wsdl」 => WSDL_CACHE_NONE在SoapClient的選項,然後我總是得到錯誤。 – raugfer

回答

0

不知道爲什麼會發生這種情況。首先調用錯誤總是發生,可能是由於WSDL加載。此後,它可以通過增加高速緩存WSDL TTL來緩解:

$ttl=ini_get('soap.wsdl_cache_ttl'); 
ini_set('soap.wsdl_cache_ttl',100*365*24*60*60); // 100 years 
$soapClient=new SoapClient($namespace.'/CampaignService?wsdl',array(
    'features'=>SOAP_SINGLE_ELEMENT_ARRAYS, 
    'encoding'=>'utf-8', 
    'cache_wsdl'=>WSDL_CACHE_BOTH, 
    'stream_context'=>stream_context_create(array(
      'http'=>array(
        'header'=>'Authorization : Bearer '.$accessToken, 
      ), 
    )), 
)); 
ini_set('soap.wsdl_cache_ttl',$ttl);