[SOS] 调用API的update未成功

8 views
Skip to first unread message

Dan Mayer

unread,
Nov 2, 2009, 11:55:38 PM11/2/09
to 嘀咕app讨论组
我的代码是基于GAE的python程序,自己做了一个调用嘀咕API的update方法,但是总不成功,请达人指点!以下是我的代码

minicloud_username = 'xxx'
minicloud_password = 'yyy'
minicloud_url = 'http://api.minicloud.com.cn/statuses/update.json'


def post(self):
content_str = json.dumps({'content' : self.request.get('text')})
auth_str = "Basic " + base64.encodestring(minicloud_username) + ":" +
base64.encodestring(minicloud_password)
headers = {"Content-type" : "text/x-json; charset=UTF-8",
"Content-length" : len(content_str ),
"Authorization" : auth_str }

result = urlfetch.fetch(minicloud_url, content_str, 'POST', headers)
self.response.out.write(result.status_code)
self.response.out.write(result.content)

代码可以正确执行,且调用fetch后,response返回的status_code=200,看帮助上说如果update成功会返回发送的文本,而
这里返回的content却为空,当然登录digu后,也没有看到发送的文字,请问这是什么原因呢?

ppluck_chen

unread,
Nov 3, 2009, 1:19:18 AM11/3/09
to 嘀咕app讨论组
Hello,如果你再发送不成功,可以找我,嘀咕帐号:magic_chen,qq:287978132

Dan Mayer

unread,
Nov 4, 2009, 6:18:58 AM11/4/09
to 嘀咕app讨论组
问题解决,两处需要修改:
1. base64.encodestring的方法加密会自动加一个'\n',应该使用base64.b64encode方法
2. 默认不用写Content-Type,正确的header的写法是
author = 'Basic' + base64.b64encode('username' + ':' + 'password')
headers={'Authorization' : author}

谢谢ppluck_chen支持!!

Reply all
Reply to author
Forward
0 new messages