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 - あなたの正しい、トークンは1時間だけ有効なので、cronで動作しません。代わりにリフレッシュトークンをどこかに保存し(DB、ファイル、ハードコード)、それを使ってcronジョブに接続してください。 –

+0

@ jkns.coありがとうございます、あなたの以前の回答が私の読み込みを助けました。 – Rob

答えて

1

前の質問hereのために@ jkns.coに感謝してくれました。

$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

うまくいけばうれしい! –

関連する問題