访问 Google 通讯录时出现 OAuth 问题
我正在尝试在以下 PHP 代码中使用 google contact API 导入某人的联系人。它使用 OAuth 1.0 协议:
<?php
$consumer_key="www.spats.in";
$secret="***********************";
$mt = microtime();
$rand = mt_rand();
$nonce = md5($mt.$rand);
$url="https://www.google.com/accounts/OAuthGetRequestToken";
$params="oauth_callback=www.spats.in/nssc2/gmailContactsImport.php".
"&oauth_consumer_key=$consumer_key".
"&oauth_nonce=$nonce".
"&oauth_signature_method=HMAC-SHA1".
"&oauth_timestamp=".time().
"&oauth_version=1.0".
"&scope=https://www.google.com/m8/feeds/";
$base_string = "GET&".urlencode($url).'?'.urlencode($params);
$signature = base64_encode(hash_hmac('sha1', $base_string, $secret, true));
$params.="&oauth_signature=".$signature;
$result=file_get_contents($url."?".$params);
echo $result;
?>
但是,在执行 php 代码时,结果如下:
signature_invalid base_string:GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dwww.spats.in%252Fnssc2%252FgmailContactsImport.php%26oauth_consumer_key%3Dwww.spats.in%26oauth_nonce%3D36bc2ce5f00b79300d753bb94dc924df%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1308644840%26oauth_version%3D1.0%26scope%3Dhttps%253A%252F%252Fwww.google.com%252Fm8%252Ffeeds%252F
签名有什么问题?我似乎无法弄清楚。
I am trying to import the contacts of a person using google contacts API in the following PHP code . It uses OAuth 1.0 protocol :
<?php
$consumer_key="www.spats.in";
$secret="***********************";
$mt = microtime();
$rand = mt_rand();
$nonce = md5($mt.$rand);
$url="https://www.google.com/accounts/OAuthGetRequestToken";
$params="oauth_callback=www.spats.in/nssc2/gmailContactsImport.php".
"&oauth_consumer_key=$consumer_key".
"&oauth_nonce=$nonce".
"&oauth_signature_method=HMAC-SHA1".
"&oauth_timestamp=".time().
"&oauth_version=1.0".
"&scope=https://www.google.com/m8/feeds/";
$base_string = "GET&".urlencode($url).'?'.urlencode($params);
$signature = base64_encode(hash_hmac('sha1', $base_string, $secret, true));
$params.="&oauth_signature=".$signature;
$result=file_get_contents($url."?".$params);
echo $result;
?>
However, on executing the php code, the result is as follows :
signature_invalid base_string:GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dwww.spats.in%252Fnssc2%252FgmailContactsImport.php%26oauth_consumer_key%3Dwww.spats.in%26oauth_nonce%3D36bc2ce5f00b79300d753bb94dc924df%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1308644840%26oauth_version%3D1.0%26scope%3Dhttps%253A%252F%252Fwww.google.com%252Fm8%252Ffeeds%252F
What is the problem with the signature? I can't seem to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,问题出在钥匙上。我自己解决了。
Okkay, the problem was with the key. I solved it myself.