0

我已经成功地管理连接到AdSense的API和运行报告。但是,每次运行它时都需要登录,因此它不会作为cron作业运行。挣扎存储令牌成为AdSense的API

我发现与此相关的一些其他问题。有些人建议服务帐户,而另一些人则指出服务帐户不适用于AdSense。建议的解决方案是在我的服务器上存储一个令牌,但我一直在努力让它工作。这是到目前为止我的代码(其工作,但需要手动登录):

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF']; 

$client = new Google_Client(); 
$client->addScope('https://www.googleapis.com/auth/adsense.readonly'); 
$client->setAccessType('offline'); 
$client->setApplicationName('My Application name'); 
$client->setClientId(' MY ID '); 
$client->setClientSecret(' MY SECRET '); 
$client->setRedirectUri($scriptUri); 
$client->setDeveloperKey(' MY KEY '); // API key 

$accountId = " MY ACCOUNT " ; 
$adClientId = " MY CLIENT " ; 


// $service implements the client interface, has to be set before auth call 
$service = new Google_Service_AdSense($client); 

if (isset($_GET['logout'])) { // logout: destroy token 
    unset($_SESSION['token']); 
    die('Logged out.'); 
} 

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session 
    $client->authenticate($_GET['code']); 
    $_SESSION['token'] = $client->getAccessToken(); 
} 

if (isset($_SESSION['token'])) { // extract token from session and configure client 
    $token = $_SESSION['token']; 
    $client->setAccessToken($token); 
} 

if (!$client->getAccessToken()) { // auth call to google 
    $authUrl = $client->createAuthUrl(); 
    header("Location: ".$authUrl); 
    die; 
} 

$startDate = '2015-11-01'; 
$endDate = 'today'; 
$optParams = array(
    'metric' => array(
    'EARNINGS'), 
    'dimension' => array('DATE'), 
    'sort' => '+DATE', 
    'filter' => array(
    'CUSTOM_CHANNEL_NAME==Mega Seating Plan' 
) 
); 
// Run report. 
$report = $service->accounts_reports->generate($accountId, $startDate, 
    $endDate, $optParams); 
if (isset($report) && isset($report['rows'])) { 


    // Get results. 
    foreach($report['rows'] as $row) { 

    $date = $row[0] ; 
    $earnings[$date] = $row[1] ; 


    } 
} else { 
    print "No rows returned.\n"; 
} 

任何人可以给我我如何可以将标记存储到上面的代码中任何指针吗?

+0

看看我的答案在这里 - https://stackoverflow.com/questions/35591224/google-cal-api-script-to-check-events/35638759 #35638759 - 您正确的,令牌不会在一个cron,因为它是唯一有效的工作一小时。相反,存储刷新令牌某处(DB,文件,硬编码),并用它来在你的cron作业连接。 –

+0

@ jkns.co谢谢你,你以前的答案帮助我的负荷。 – Rob

回答

1

谢谢@ jkns.co前一个答案here这让我得到它的工作。

这是我的最终代码:

$scriptUri = "I HAD TO PUT MY ABSOLUTE URL HERE, OTHERWISE THE CRON JOB WOULD LOOK IN THE WRONG PLACE" ; 

$client = new Google_Client(); 
$client->addScope('https://www.googleapis.com/auth/adsense.readonly'); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt ("force"); // This line had to be added to force the approval prompt and request a new token 
$client->setApplicationName('My Application name'); 
$client->setClientId('BLAH'); 
$client->setClientSecret('BLAH'); 
$client->setRedirectUri($scriptUri); 
$client->setDeveloperKey('BLAH'); // API key 

$accountId = "BLAH" ; 
$adClientId = "BLAH" ; 


// $service implements the client interface, has to be set before auth call 
$service = new Google_Service_AdSense($client); 

if (isset($_GET['logout'])) { // logout: destroy token 
    unset($_SESSION['token']); 
    die('Logged out.'); 
} 

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session 
    $client->authenticate($_GET['code']); 
    $_SESSION['token'] = $client->getAccessToken(); 

    // If it successfully authenticates, I request the refresh token 
    $refreshToken = $client->getRefreshToken(); 

    storeRefreshToken($refreshToken) ; // This function stores the token in MySQL 
} 

else {  // Otherwise it loads the refresh token from MySQL 


    $refreshToken = getRefreshToken() ; 



    $client->refreshToken($refreshToken); 
    $_SESSION['token'] = $client->getAccessToken(); 

} 

if (isset($_SESSION['token'])) { // extract token from session and configure client 
    $token = $_SESSION['token']; 
    $client->setAccessToken($token); 


} 

if (!$client->getAccessToken()) { // auth call to google 
    $authUrl = $client->createAuthUrl(); 
    header("Location: ".$authUrl); 
    die; 
} 
+0

很高兴你的工作! –