urllib2.HTTPError: HTTP Error 401 while querying using the new Bing API ( in azure marketplace )

放肆的年华 提交于 2019-12-04 09:25:50

So, here's the working code. The problem I was creating is the query keywords' format.

 queryBingFor = "'google fibre'" # the apostrophe's required as that is the format the API Url expects. 
 quoted_query = urllib.quote(queryBingFor)

 rootURL = "https://api.datamarket.azure.com/Bing/Search/"
 searchURL = rootURL + "Image?$format=json&Query=" + quoted_query

 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
 password_mgr.add_password(None, searchURL,username,accountKey)

 handler = urllib2.HTTPBasicAuthHandler(password_mgr)
 opener = urllib2.build_opener(handler)
 urllib2.install_opener(opener)
 readURL = urllib2.urlopen(searchURL).read()

This should give the results in the respective JSON format. As I'm using urllib2's httpbasicauthhandler, the password's converted into base64 implicitly, I presume.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!