
我目前正在開發一個 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()