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

很高興你的工作! –