4

そのため、スタックオーバーフローの同じ屋根の下でほとんどの回答に基づいて修正を行いましたが、まだこの問題を解決できません。

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

問題が何であるかについての手がかりはありますか?

ありがとう!

4

1 に答える 1

3

だから、これが動作するコードです。私が作成していた問題は、クエリキーワードの形式です。

 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に変換されていると思います。

于 2012-07-31T14:47:56.637 に答える