AdWords API已过期Cookie错误

时间:2014-02-21 18:18:36

标签: php authentication cookies adwords-api-v201109

我正在尝试构建一个“尽可能简单”的例程来检索AdWords广告系列名称。

但是,我收到此错误消息

  

AuthenticationError.LOGIN_COOKIE_REQUIRED @;触发: '<空>'

每当我第一次运行以下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);

1 个答案:

答案 0 :(得分: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);