所以,我已经做出基于大多数堆栈溢出在同一屋檐下的答案更正,我仍然无法解决这个问题。
queryBingFor = "Google Fibre"
quoted_query = urllib.quote(queryBingFor)
account_key = "dslfkslkdfhsehwekhrwkj2187iwekjfkwej3"
rootURL = "https://api.datamarket.azure.com/Bing/Search/v1/"
searchURL = rootURL + "Image?format=json&Query=" + quoted_query
cred = base64.encodestring(accountKey)
reqBing = urllib2.Request(url=searchURL)
author = "Basic %s" % cred
reqBing.add_header('Authorization',author)
readURL = urllib2.urlopen(reqBing)
我知道我错过了在上面的代码什么的,这给了我一个:
urllib2.HTTPError: HTTP Error 401: The authorization type you provided is not supported. Only Basic and OAuth are supported
对这个问题可能是什么任何线索?
谢谢!
所以,这里的工作代码。 我创建的问题是查询关键词格式。
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()
这应该给于相应的JSON格式的结果。 由于我使用的urllib2的httpbasicauthhandler,密码的转换为base64,含蓄,我相信。
文章来源: urllib2.HTTPError: HTTP Error 401 while querying using the new Bing API ( in azure marketplace )