duan19911992 2011-06-23 18:30
浏览 30

使用PHP的Google OAuth流程中的问题

I am using OAuth 1.0 protocol to connect to Google Contacts service and fetch all contacts of a user. When I am requesting for the Request Token, one out of 3 times I am getting a 'signature_invalid' response from the Google Servers. How could this be possible:the same code works many time and sometimes it doesn't? If anyone wants to try, here its the test page : http://www.spats.in/nssc2/gmailContactsImport.php. In the first lines, it prints the base string that I have constructed. On the second line, it prints the response from the server. On the third line it prints the request secret if it receives it from the server, otherwise its gibberish. Try reloading the page a few time; you'll definitely get a signature_invalid response a few time.

Will this problem persist and I should take care to make changes in my code to resend the request in case a negative response is received, or am I doing something wrong?

Here is my code if anyone wants to have a look at it:

$_SESSION['uid']=31;  // Just set to something for checking
$consumer_key="www.spats.in";
$secret="***********************";  //I've hidden it
$mt = microtime();$rand = mt_rand(); $nonce = md5($mt.$rand);
$time=time();

$key=urlencode(utf8_encode($secret))."&";
$url="https://www.google.com/accounts/OAuthGetRequestToken";
$params="oauth_callback=".urlencode("http://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=".urlencode("https://www.google.com/m8/feeds/");

$base_string = "GET&".urlencode($url).'&'.rawurlencode($params);
$signature = base64_encode(hash_hmac('sha1', $base_string, $key, true));

//Send off request
$params.="&oauth_signature=".$signature;
$result=file_get_contents($url."?".$params);

//Parse the result
$tokens=explode("&",$result);
$pos['oauth_token']=strpos($tokens[0],"=")+1;
$oauth_token=substr($tokens[0],$pos['oauth_token']);
$pos['oauth_secret']=strpos($tokens[1],"=")+1;
$oauth_secret=substr($tokens[1],$pos['oauth_secret']);

//Save request secret
$num=mysql_query("INSERT INTO google_auth_tokens (id,request_token_secret) values($_SESSION[uid],'$oauth_secret')");


$authorizeURL="https://www.google.com/accounts/OAuthAuthorizeToken";
header("Location:".$authorizeURL."?oauth_token=".$oauth_token);

UPDATED : As you can see if you visit the page I mentioned above and get an error, the base string constructed by me and Google exactly match everytime, so it means there may be some problem with the way I am signing the base string. I can't catch the problem in that. Can anyone detect any problem in that?

UPDATED : Here are two base strings, the former generated a 'signature_invalid' response and latter got through and gave back the request token :

GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dhttp%253A%252F%252Fwww.spats.in%252Fnssc2%252FgmailContactsImport.php%26oauth_consumer_key%3Dwww.spats.in%26oauth_nonce%3D57e4656240c67b1e6a5bcbd5f9ecb2cf%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1308856148%26oauth_version%3D1.0%26scope%3Dhttps%253A%252F%252Fwww.google.com%252Fm8%252Ffeeds%252F

GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dhttp%253A%252F%252Fwww.spats.in%252Fnssc2%252FgmailContactsImport.php%26oauth_consumer_key%3Dwww.spats.in%26oauth_nonce%3D734bab3909fea245a2ebcfefcea36c54%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1308856162%26oauth_version%3D1.0%26scope%3Dhttps%253A%252F%252Fwww.google.com%252Fm8%252Ffeeds%252F

Both are exactly same, except for the nonce and timestamp parameters, which ought to differ due to being sent at different times. I can't understand why one should generate an invalid signature while the other not.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么