我正在使用 ClientLogin 方法和 cURL 登录到谷歌 API。这工作正常,我收到一个令牌以供进一步使用。我现在可以使用查询 docs.google.com
$curl = curl_init();
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
curl_setopt($curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
这工作正常,我得到了我的 google 文档帐户中所有可用文档的列表。但是,如果我尝试使用从api 文档中获得的 URL 对电子表格.google.com 进行相同的查询:
https://spreadsheets.google.com/feeds/spreadsheets/private/full
我收到 401 错误,提示使用的令牌无效。在这两种情况下,我都使用相同的令牌和查询。谷歌电子表格 api 是否需要不同的令牌?
编辑:这就是我请求令牌的方式:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "my email",
"Passwd" => "my password",
"service" => "writely",
"source" => "my application name"
);
$curl = curl_init($clientlogin_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
curl_close($curl);