我正在尝试发出基本的UbuntuOne API调用。


https://one.ubuntu.com/developer/account_admin/auth/otherplatforms所述,我获取了OAUTH令牌,然后将其传递给UbuntuOne服务。
我得到了令牌和消费者信息
然后,我尝试发出/ api / file_storage / v1 API调用(请参阅:https://one.ubuntu.com/developer/files/store_files/cloud.)。该请求是使用OAUTH令牌签名的。


下面的代码段是我正在执行的确切代码(减去email.password / description字段。)令牌和使用者数据已正确返回。发出/ api / file_storage / v1请求时,我从服务器收到“ 401 UNAUTHORIZED”字样……为什么?

import base64
import json
import urllib
import urllib2
import oauth2

email = 'bla'
password = 'foo'
description = 'bar'

class Unauthorized(Exception):
  """The provided email address and password were incorrect."""

def acquire_token(email_address, password, description):
  """Aquire an OAuth access token for the given user."""
  # Issue a new access token for the user.
  request = urllib2.Request(
    'https://login.ubuntu.com/api/1.0/authentications?' +
    urllib.urlencode({'ws.op': 'authenticate', 'token_name': description}))
  request.add_header('Accept', 'application/json')
  request.add_header('Authorization', 'Basic %s' % base64.b64encode('%s:%s' % (email_address, password)))
  try:
    response = urllib2.urlopen(request)
  except urllib2.HTTPError, exc:
    if exc.code == 401: # Unauthorized
      raise Unauthorized("Bad email address or password")
    else:
      raise
  data = json.load(response)
  consumer = oauth2.Consumer(data['consumer_key'], data['consumer_secret'])
  token = oauth2.Token(data['token'], data['token_secret'])

  # Tell Ubuntu One about the new token.
  get_tokens_url = ('https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/')
  oauth_request = oauth2.Request.from_consumer_and_token(consumer, token, 'GET', get_tokens_url)
  oauth_request.sign_request(oauth2.SignatureMethod_PLAINTEXT(), consumer, token)
  request = urllib2.Request(get_tokens_url)
  for header, value in oauth_request.to_header().items():
    request.add_header(header, value)
  response = urllib2.urlopen(request)

  return consumer, token

if __name__ == '__main__':
  consumer, token = acquire_token(email, password, description)
  print 'Consumer:', consumer
  print 'Token:', token

  url = 'https://one.ubuntu.com/api/file_storage/v1'

  oauth_request = oauth2.Request.from_consumer_and_token(consumer, token, 'GET', url)
  oauth_request.sign_request(oauth2.SignatureMethod_PLAINTEXT(), consumer, token)

  request = urllib2.Request(url)
  request.add_header('Accept', 'application/json')

  for header, value in oauth_request.to_header().items():
    request.add_header(header, value)

  response = urllib2.urlopen(request)

最佳答案

问题在于“说明”字段。必须采用以下格式:

Ubuntu One @ $hostname [$application]


否则,UbuntuOne服务将返回“ ok 0/1”,并且不会注册令牌。

07-24 18:36
查看更多