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

时间:2011-06-21 08:40:10

标签: php google-contacts

我正在尝试使用以下PHP代码中的Google通讯录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 

签名有什么问题?我似乎无法弄明白。

1 个答案:

答案 0 :(得分:1)

Okkay,问题在于密钥。我自己解决了。