gpt4 book ai didi

php - 尝试登录 Google 以下载 Google 趋势数据

转载 作者:可可西里 更新时间:2023-10-31 22:15:00 27 4
gpt4 key购买 nike

我正在尝试:

  1. 登录谷歌
  2. 从 Google 趋势下载 CSV 数据

我在 (1) 中取得了成功,但在 (2) 中没有取得成功。我从 Google 返回了一个授权 token ,并将其与随后的请求一起发送给趋势,但谷歌随后返回错误:“您必须登录才能从 Google 趋势导出数据”:

// http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
$data = array(
'accountType' => 'GOOGLE',
'Email' => 'my.email@gmail.com',
'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 Trends 的授权 token 没有被接受或忽略。我不知道到底发生了什么,因为没有给出额外的错误信息。

有没有人看到我做错了什么?如果你能让它工作,这意味着谷歌正在返回 CSV 数据,那么赏金就是你的了,我们都有一份迟到的圣诞礼物:-)


所以我发现问题与 cURL 无关。我所做的是:

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

换句话说,我正在发送 Google 在 http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html 上定义的 header 但没有运气获得适当的返回。互联网上*没有*关于此的信息。谁知道这里有什么问题?

最佳答案

检查您的代码后,问题是 Google Trends 需要 SID key 而不是 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 ,一个 cURL 包装器。

关于php - 尝试登录 Google 以下载 Google 趋势数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8626362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com