【问题标题】:Chrome extension OATH 401 error(Unauthorized)Chrome 扩展 OATH 401 错误(未经授权)
【发布时间】:2011-08-25 10:27:24
【问题描述】:

我正在使用 JS 来访问 rdio 插件。我将以下内容用于 Oauth http://code.google.com/chrome/extensions/tut_oauth.html。 我能够获得签名的令牌等。但是,当我尝试在http://api.rdio.com/1/ 发送签名请求时,我会收到 401,未经授权的错误。

X-Mashery-错误代码:ERR_401_INVALID_SIGNATURE X-Mashery-Responder:mashery-web4.LAX

这是我要发送的内容:

 var url = 'http://api.rdio.com/1/';
 var request = {
   'method': 'POST',
   'headers': {
     'Content-Type': 'application/x-www-form-urlencoded'
   },    
   'parameters': {
     'alt': 'json',
     'method':'currentUser'
   },
   'body': 'Data to send'
 };

 bgPage.oauth.sendSignedRequest(url, mycallback, request);  

我在控制台中收到以下错误。

Request URL:http://api.rdio.com/1/?alt=json&method=currentUser&oauth_consumer_key=yv8ehzehdv55**********&oauth_nonce=******&oauth_signature=**********&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1305190893&oauth_token=us6myp99p4qc86umea9p8fp*****************
Request Method:POST
Status Code:401 Unauthorized
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:12
Content-Type:application/x-www-form-urlencoded
Cookie:__qca=P0-158278476-1296771701175; r=eyJfdSI6IDE5MjY1LCAiX2UiOiAzMTU1NjkyNn0.SvN8xd7rIuLzTp7hxqi4eJEdvu8; __utmz=225830489.1305153361.198.18.utmcsr=rdioquiz.ianloic.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=225830489.281668250.1296676147.1305184513.1305187119.201; __utmc=225830489
Host:api.rdio.com
Origin:chrome-extension://oiojbkkpmcgmpnjkhjmaggajckamjkap
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_6) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24
Query String Parameters
alt:json
method:currentUser
oauth_consumer_key:yv8ehzehdv55pbb74ss9dt23
oauth_nonce:BQF0x
oauth_signature:KttF************tRO 8PL yjPF2Ktk=
oauth_signature_method:HMAC-SHA1
oauth_timestamp:1305190893
oauth_token:us6myp99p4qc86umea9p8fphbgq4dxdd76txvyn***********
Form Data
Data to send:
Response Headers
Accept-Ranges:bytes
Content-Length:30
Content-Type:text/xml
Date:Thu, 12 May 2011 09:01:33 GMT
Server:Mashery Proxy
X-Mashery-Error-Code:ERR_401_INVALID_SIGNATURE
X-Mashery-Responder:mashery-web4.LAX    

*我只是想模仿这里提到的内容。它是一个来自 Google 的 Oauth 库(http://code.google.com/chrome/extensions/tut_oauth.html),用于简化 Chrome 扩展开发。 他们有一个 Oauth 示例代码来获取您的文档列表等。http://code.google.com/chrome/extensions/samples.html#4e35caa9742fb82dbd628892d23a781614f6eff6 我想我无法通过向 rdio API 发送 POST 请求。它给出了一个未经授权的错误。*

【问题讨论】:

  • OAuth 是出了名的难以调试,您可以使用hueniverse.com/2008/10/… 上的应用程序来比较签名与您的代码输出的内容吗?
  • 谢谢,这对调试很有帮助。

标签: javascript oauth google-chrome-extension


【解决方案1】:

我们发现相同服务 (rdio) 和方法 ("currentUser") 存在类似问题。

最终起作用的是: (1) 确保您在 POST 正文中有 method=currentUser ;如果是这种情况,我不确定上面的 curl 输出。

而且,这是真正解决问题的一点: (2) 我们还必须将方法名称添加到签名本身。

仅供参考,我们使用了这个库:https://oauth.googlecode.com/svn/code/javascript/

但是,正如您所看到的,棘手的部分是弄清楚如何在创建签名的库中播种方法。如果没有 'method=currentUser' 作为签名的一部分,我们会遇到相同的错误情况。

【讨论】:

    【解决方案2】:

    在您的计算机上检查您的时区、日期和时间。如果其中任何一个错误,OAuth 将失败。

    【讨论】:

    • 这与 Oauth 有什么关系?
    • OAuth 使用“nonce”,即只能使用一次的数字。因此,提供者需要跟踪大量的随机数。为了限制提供者需要记住的随机数的数量,OAuth 随机数仅在一定时间内有效,之后提供者可以忘记它们。更多信息请点击此处:hueniverse.com/oauth/guide/security
    猜你喜欢
    • 1970-01-01
    • 2015-04-28
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2014-03-10
    • 2014-06-24
    相关资源
    最近更新 更多