2014-03-30 72 views
1

我試圖通過python訪問帳戶信息,這是OAuth 1.0。我有下面的代碼:AttributeError:使用Python時,'元組'對象沒有屬性'startswith'httplib

token_url = "oauth/token?" + urlencode(ksort(params)) + "&sig=%s",sig 
conn = httplib.HTTPSConnection(url, 443) 
conn.request("GET", token_url) 

這報道:

Traceback (most recent call last): 
    File "C:\Users\windows\Desktop\https.py", line 35, in <module> 
    conn.request("GET", token_url) 
    File "C:\Python27\lib\httplib.py", line 973, in request 
    self._send_request(method, url, body, headers) 
    File "C:\Python27\lib\httplib.py", line 1001, in _send_request 
    self.putrequest(method, url, **skips) 
    File "C:\Python27\lib\httplib.py", line 900, in putrequest 
    if url.startswith('http'): 
AttributeError: 'tuple' object has no attribute 'startswith' 

連接正常,但我無法通過以下HTTPS請求去。

回答

2

要傳遞的,而不是一個字符串的元組:

>>> sig = 'sig' 
>>> 'oauth/token?blahblah&' + 'sig=%s', sig 
('oauth/token?blahblah&sig=%s', 'sig') 
>>> 'oauth/token?blahblah&' + 'sig=%s' % sig 
'oauth/token?blahblah&sig=sig' 

替換以下行:

token_url = "oauth/token?" + urlencode(ksort(params)) + "&sig=%s",sig 
                   ^

token_url = "oauth/token?" + urlencode(ksort(params)) + "&sig=%s"%sig 
相關問題