【问题标题】:Not being able to authenticate Google Visualization Query Language with given Authentication Key无法使用给定的身份验证密钥对 Google 可视化查询语言进行身份验证
【发布时间】:2011-07-25 07:31:09
【问题描述】:

无法验证 Google 电子表格 API。 我正在使用从here 窃取的代码,

$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
    "accountType" => "HOSTED_OR_GOOGLE",
    "Email"   => "MY_EMAIL@GOOGLE.COM",
    "Passwd"  => "MY_EMAIL_PASS",
    "service" => "writely",
    "source"  => "MY_APPLICATION_NAME"
);

// Initialize the curl object
$curl = curl_init($clientlogin_url);

// Set some options (some for SHTTP)
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);

// Execute
$response = curl_exec($curl);

// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];

echo "The auth string is: " . $auth; //this worked!

所以这行得通,现在我有一个密钥但实际上使用它......下面的代码给了我一个用户未通过身份验证的错误:

$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
);

$key = 'MY_KEY';

// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://spreadsheets.google.com/tq?tqx=version:0.6;out:json&key='.$key);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);

$response = curl_exec($curl);
curl_close($curl);


var_dump ($response);

这给了我

string(272) "google.visualization.Query.setResponse({version:'0.6',status:'error',errors:[{reason:'user_not_authenticated',message:'User not signed in',detailed_message:'\u003ca target=\u0022_blank\u0022 href=\u0022http://spreadsheets.google.com/\u0022\u003eSign in\u003c/a\u003e'}]});"

显然它无法使用Google Visualization Query Language 的身份验证密钥。我做错了什么?

非常感谢!

【问题讨论】:

    标签: php authentication curl gdata-api google-sheets


    【解决方案1】:

    我会尝试在使用 google 身份验证字符串的代码中进行以下修改:

    添加urlencode

    $headers = array(  
       "Authorization: GoogleLogin auth=" . urlencode($auth),  
       "GData-Version: 3.0",  
    );
    

    并跳过验证 SSL 证书:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    

    我在post 中描述了我使用 CURL 实现身份验证的经验。在那里你会发现一些调试 CURL 请求的技巧。

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多