Вопрос

I am trying to build an "as simple as possible" routine to retrieve AdWords campaign names.

However, I am getting this error message

AuthenticationError.LOGIN_COOKIE_REQUIRED @ ; trigger:'<null>'

whenever I run the following PHP script for the first time.

Subsequent runs work fine for some hours until I get the error again (I believe the referred cookie expires again).

What is this cookie about? Can someone pinpoint what is going on and how to fix it?

<?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);
Это было полезно?

Решение

Not sure why this happens. First call the error always happens, probably due to WSDL loading. Afterwards it can be mitigated by increasing the WSDL cache 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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top