2011-12-24 87 views
11

我想:試圖登錄到谷歌爲了下載谷歌趨勢數據

  1. 登錄到谷歌
  2. 從谷歌趨勢

下載CSV數據我成功(1 )而不是(2)。我得到恢復,從谷歌的授權令牌,並正與後續請求趨勢發送它,但儘管如此谷歌隨後會返回一個錯誤:「您必須先登錄輸出從谷歌趨勢的數據」:

// http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html 
$data = array(
    'accountType' => 'GOOGLE', 
    'Email'  => '[email protected]', 
    'Passwd'  => 'my.password', 
    'service'  => 'trendspro', 
    'source'  => 'company-application-1.0' 
); 

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $response = curl_exec($ch); 

    preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches); 

    // We now have an authorization-token 
    $headers = array(
    "Authorization: GoogleLogin auth=" . $matches[1], 
    "GData-Version: 3.0" 
); 

    curl_setopt($ch, CURLOPT_URL, "http://www.google.com/trends/viz?q=MSFT&date=2011-2&geo=all&graph=all_csv&sort=0&sa=N"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, false); 
    $csv = curl_exec($ch); 
curl_close($ch); 

// Returns : "You must be signed in to export data from Google Trends" 
// Expected: CSV data stream 
print_r($csv); 

出於某種原因,我發送給Google趨勢的身份驗證令牌沒有被接受或忽略。我不知道會發生什麼,因爲沒有提供額外的錯誤信息。

有沒有人看到我做錯了什麼?如果你能得到它的工作,這意味着谷歌正在返回CSV數據,那麼賞金是你的,我們都擁有一個遲到的聖誕禮物:-)


所以我想通了這個問題無關與捲曲。我做的是:

SID=DQAAAMUAAADMqt...aYPaYniC_iW 
LSID=DQAAAMcAAACI5...YDTBDt_xZC9 
Auth=DQAAAMgAAABm8...trXgqNv-g0H 
GData-Version: 3.0  
Authorization: GoogleLogin auth=DQAAAMgAAABm8...trXgqNv-g0H 
  • 我得到恢復:

標題:

Date: Tue, 27 Dec 2011 00:17:20 GMT 
Content-Encoding: gzip 
Content-Disposition: filename=trends.csv 
Content-Length: 97 
X-XSS-Protection: 1; mode=block 
Server: Google Trends 
X-Frame-Options: SAMEORIGIN 
Content-Type: text/csv; charset=UTF-8 
Cache-Control: private 

數據:

You must be signed in to export data from Google Trends 

換句話說,我要送頭由谷歌在http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html定義,但沒有運氣得到合理的回報。 Interwebs上有關於此的* no *信息。誰知道問題在這裏?

+0

你在這裏檢查http://stackoverflow.com/questions/4986758/oauth-google-trends-download-csv-file – Pateman 2011-12-26 17:34:57

+0

感謝您的提示。您的鏈接將我帶到http://stackoverflow.com/questions/1656446/download-csv-from-google-insight-for-search,但這也不能解決問題。我正在按照那裏的解釋做,但沒有運氣,我看不到我在做什麼錯... – Pr0no 2011-12-26 19:47:08

+0

不知道這是否有幫助,但有一個人使用cURL訪問HTTPS網站時遇到問題,他的問題在這裏解決:http://stackoverflow.com/questions/316099/cant-connect-to-https-site-using-curl-returns-0-length-content-instead-what-c​​ – Pateman 2011-12-26 20:00:22

回答

4

在檢查您的代碼後,問題是Google Trends需要SID密鑰而不是Auth。這是我寫的下載代碼的CSV的

<?php 

header('content-type: text/plain'); 

// Set account login info 
$data['post'] = array(
    'accountType' => 'HOSTED_OR_GOOGLE', // indicates a Google account 
    'Email'  => '', // full email address 
    'Passwd'  => '', 
    'service'  => 'trendspro', // Name of the Google service 
    'source'  => 'codecri.me-example-1.0' // Application's name, e.g. companyName-applicationName-versionID 
); 

$response = xhttp::fetch('https://www.google.com/accounts/ClientLogin', $data); 

// Test if unsuccessful 
if(!$response['successful']) { 
    echo 'response: '; print_r($response); 
    die(); 
} 

// Extract SID 
preg_match('/SID=(.+)/', $response['body'], $matches); 
$sid = $matches[1]; 

// Erase POST variables used on the previous xhttp call 
$data = array(); 

// Set the SID in cookies 
$data['cookies'] = array(
    'SID' => $sid 
); 

該使用我xhttp class,捲曲包裝。

+0

您的xhttp類別連結會重新導向至隨機廣告網頁 – Jones03 2015-04-02 03:10:35

0

嗯,我還沒有使用谷歌API,但想進入谷歌應用程序API爲即將到來的項目,所以開始挖掘。我的猜測是,即使您正確/成功進行身份驗證(由響應的身份驗證部分進行驗證),因爲趨勢並不在the list of services that implement the Google Data Protocol中,但Google不會兌現趨勢的身份驗證令牌(再次這是最佳猜測!)。

我的想法是,您需要使用傳統的方法登錄到Google Trends並下載CSV,也就是說,您的客戶端而不是應用程序的瀏覽器就像CSV一樣。不確定,但我發現an older python client on github聲稱可以從Google Trends下載CSV。還有a blog post on the client。您可能能夠將其逆向工程化爲一個PHP等效物,祝您好運!

0

似乎Google並未正式容忍任何基於腳本的趨勢使用。這可以解釋爲什麼你的auth失敗,因爲它不接受api連接。嘗試使用Web客戶端庫來捕獲cookie並使用它來收集數據。這是之前鏈接的解決方案python client on git

在潛在無關的說明中,您使用了服務trendspro,但這是Google洞察的服務名稱。嘗試只是服務=>'趨勢'

2

合適的工具爲正確的工作,你認爲PhantomJS

它可能更具可讀性。