本文介绍了urllib2.HTTPError:HTTP错误401,而使用新的必应API(在蔚蓝的市场)查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

所以,我已经做出基于大多数栈溢出同一屋檐下的答案更正,我仍然无法解决这个问题。

So, I ve made corrections based on most of the answers under the same roof in stack overflow, I'm still unable to resolve this problem.

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)

我知道我错过了在上面code的东西,这给了我一个:

I know I'm missing out something in the above code, that gives me a:

urllib2.HTTPError: HTTP Error 401: The authorization type you provided is not supported.  Only Basic and OAuth are supported

在这个问题可能是什么任何线索?

Any clue on what the problem could be?

谢谢!

推荐答案

所以,这里的工作code。我创建的问题是查询关键词格式。

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()

这应该给在各自的JSON格式的结果。由于我使用的urllib2的httpbasicauthhandler,密码的转换成的base64含蓄,我presume。

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.

这篇关于urllib2.HTTPError:HTTP错误401,而使用新的必应API(在蔚蓝的市场)查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:02