4

그래서 스택 오버플로에서 같은 지붕 아래에서 대부분의 답변을 기반으로 수정했습니다. 아직 해결할 수 없습니다. 문제. 문제가 될 수 있는지에urllib2.HTTPError : 새로운 Bing API (하늘색 시장에서)를 사용하여 쿼리하는 동안 HTTP 오류 401

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

단서 :

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) 

나는 그것이 나에게를 제공, 나는 위의 코드에서 뭔가를 누락 알아?

감사합니다.

답변

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로 변환됩니다.

+0

사용자 이름 변수의 의미는 무엇입니까? – varagrawal