2016-09-10 1 views
0

부호화 UM 그것을 파이썬 인코딩 공간이 잘못</p> <pre><code>params = { 'response_type': 'token', 'client_id': o_auth_client_id, 'redirect_url': call_back_url, 'scope': 'activity heartrate location' } print urllib.urlencode(params) </code></pre> <p>다음과 같이 I는 사전을

하지만 결과

REDIRECT_URL = HTTP % 3A % 2F % 2F127.0.0에

를 인코딩하는 .1 % 3A8084 % 2Fagile_healtg % 2Faccess_token % 2F & response_type = 토큰 & client_id = xxxxxx & scope = 활동 + heartrate + 위치

음 점점 불행하게도 공간 + 징후

로 인코딩됩니다하지만 결과는 내가 얻을 수있는 방법

범위 = 활동 % 20nutrition %의 20heartrate

해야 이상과 같이 공백에 대한 올바른 인코딩 파이썬?

+0

이 쿼리에서 공간에 대한 올바른 인코딩입니다. 'http://www.google.com/?'을 사용해보십시오. + urllib.urlencode ({ "q": "http 쿼리 문자열"})'. – zvone

+0

@zvone +가 올바르지 않으므로 공백이 % 20 인코딩 공간으로 필요합니다. – Kalanamith

+1

하지만 올바르지 않습니다. '+'는 쿼리 문자열 매개 변수의 공백을위한 올바른 인코딩입니다. '% 20'은 경로 요소의 공백을위한 인코딩입니다. –

답변

1

이 프로그램은 당신을 위해 무엇을 물어 할 수의 quote 방법을 사용해야합니다.

import urllib 

def my_special_urlencode(params): 
    return '&'.join('{}={}'.format(urllib.quote(k, ''), urllib.quote(v, '')) for k,v in params.items()) 

params = { 
     'response_type': 'token', 
     'client_id': 'xxxxxx', 
     'redirect_url': 'http://example.com/callback', 
     'scope': 'activity heartrate location' 

} 

print my_special_urlencode(params) 

결과 :

redirect_url=http%3A%2F%2Fexample.com%2Fcallback&response_type=token&client_id=xxxxxx&scope=activity%20heartrate%20location 
+0

예 해결책입니다. – Kalanamith

1

urlencode에 대한 설명서를 확인하십시오.

quote_plus 메서드는 키 값 전달시 공백을 더하기 기호로 변경하는 데 사용됩니다. unquote_plus 메서드를 사용하여 더하기 기호를 제거한 다음 quote을 사용하여 원하는 형식으로 인코딩 할 수 있습니다.

당신은 기본적 매개 변수

+0

이것이 내가 가진 것입니다. redirect_url = http % 3A % 2F % 2F127.0.0.1 % 3A8084 % 2Fagile_healtg % 2Faccess_token % 2F & response_type = 토큰 및 클라이언트 _id = xxxx & scope = activity % 2Bheartrate % 2Blocation % 20이 없습니다 .Gorge D – Kalanamith

+1

URL은 다음과 같습니다. 'urlencode' 메쏘드와 다른 매개 변수를 사용하여'quote'를 사용하여 인코딩하십시오. –