我正在编写一个简单的 python 脚本,它将使用 OSCAR protocol 与 AIM 服务器交互。 。它包括一个有点复杂的握手协议(protocol)。您本质上必须向特定 URL 发送 GET 请求,接收 XML 或 JSON 编码的回复,提取特殊的 session token 和 key ,然后使用 token 和 key 生成响应。
我试图关注these steps到发球台,但该过程在最后一个过程中失败了。这是我的代码:
class simpleOSCAR:
def __init__(self, username, password):
self.username = username
self.password = password
self.open_aim_key = 'whatever'
self.client_name = 'blah blah blah'
self.client_version = 'yadda yadda yadda'
def authenticate(self):
# STEP 1
url = 'https://api.screenname.aol.com/auth/clientLogin?f=json'
data = urllib.urlencode( [
('k', self.open_aim_key),
('s', self.username),
('pwd', self.password),
('clientVersion', self.client_version),
('clientName', self.client_name)]
)
response = urllib2.urlopen(url, data)
json_response = simplejson.loads(urllib.unquote(response.read()))
session_secret = json_response['response']['data']['sessionSecret']
host_time = json_response['response']['data']['hostTime']
self.token = json_response['response']['data']['token']['a']
# STEP 2
self.session_key = base64.b64encode(hmac.new(self.password, session_secret, sha256).digest())
#STEP 3
uri = "http://api.oscar.aol.com/aim/startOSCARSession?"
data = urllib.urlencode([
('a', self.token),
('clientName', self.client_name),
('clientVersion', self.client_version),
('f', 'json'),
('k', self.open_aim_key),
('ts', host_time),
]
)
urldata = uri+data
hashdata = "GET&" + urllib.quote("http://api.oscar.aol.com/aim/startOSCARSession?") + data
digest = base64.b64encode(hmac.new(self.session_key, hashdata, sha256).digest())
urldata = urldata + "&sig_sha256=" + digest
print urldata + "\n"
response = urllib2.urlopen(urldata)
json_response = urllib.unquote(response.read())
print json_response
if __name__ == '__main__':
so = simpleOSCAR("aimscreenname", "somepassword")
so.authenticate()
我从服务器收到以下响应:
{ "response" : {
"statusCode":401,
"statusText":"Authentication Required. statusDetailCode 1014",
"statusDetailCode":1014,
"data":{
"ts":1235878395
}
}
}
我尝试以各种方式对其进行故障排除,但我生成的 URL 看起来与 signon flow example 中显示的 URL 相同。 。然而,它失败了。
知道我在这里做错了什么吗?我对值进行哈希处理是否错误?我是否对某些内容进行了不正确的编码?我的 session 超时了吗?
最佳答案
尝试使用Twisted's OSCAR support而不是自己写?它没有经过太多维护,但我相信它有效。
关于python - 需要身份验证 - 使用 Python 建立 AIM OSCAR session 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/599218/