访问 Google 通讯录时出现 OAuth 问题

发布于 2024-11-16 07:40:12 字数 1450 浏览 3 评论 0原文

我正在尝试在以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-11-23 07:40:12

好吧,问题出在钥匙上。我自己解决了。

Okkay, the problem was with the key. I solved it myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文