我目前正在开发一款 ubuntuone Bada 应用。我可以通过 api 获取令牌,但如果我尝试请求 api 的任何其他部分,我只会得到一个空的浏览器窗口,或者在应用的 http 侦听器中不会触发任何事件。
我的请求网址如下:
https://one.ubuntu.com/api/file_storage/v1?oauth_consumer_key=abc&oauth_token=def&oauth_nonce=xdobeqcqyfjnzjsh&oauth_timestamp=1328656660424&oauth_signature_method=PLAINTEXT&oauth_version=1.0&oauth_signature=uvw%26xyz
我在各个网站上找到了这些参数,但不确定它们是否有效。
感谢您的帮助!!
答案1
您需要使用您必须的令牌详细信息签署请求使用 OAuth 协议。
这是一个在 Ubuntu 上运行的 Python 脚本示例,它将对 URL 进行签名,然后打印出该 URL;如果您随后请求该 URL,它就会起作用。
如果这仍然有问题,请告诉我。(注意:API 将数据作为 content-type 返回application/json
,因此可能无法在移动浏览器中加载。)
import oauth, urlparse, sys
from ubuntuone.couch.auth import *
if __name__ == "__main__":
# If you already have token details, then use them here; you'll need
# access_token, token_secret, consumer_key, and consumer_secret. This
# script fetches them from a running Ubuntu instead.
try:
credentials = get_oauth_credentials()
except CredentialsNotFound:
print "COULDN'T GET CREDENTIALS"
sys.exit()
access_token = credentials['token']
token_secret = credentials['token_secret']
consumer_key = credentials['consumer_key']
consumer_secret = credentials['consumer_secret']
# Now we have token details; let's use them to sign a request.
token = get_oauth_token(access_token, token_secret)
consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
url = "https://one.ubuntu.com/api/file_storage/v1"
request_body = ""
signature_method = HMAC_SHA1
parameters = {}
query = urlparse.urlparse(url)[4]
for key, value in urlparse.parse_qs(query).items():
parameters[key] = value[0]
request_len = len(request_body) if request_body else 0
timeout = 10 * (request_len / 1024 / 1024 + 1) # 10 seconds per megabyte
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
http_url=url,
http_method="GET",
oauth_consumer=consumer,
token=token,
parameters=parameters)
oauth_request.sign_request(signature_method, consumer, token)
print oauth_request.to_url()