2013-03-29 27 views
1

我被困在'獲取訪問令牌'部分。在文檔中,我需要一個'代碼'。但是,我如何獲得授權碼?我正在使用python。如何獲得Python中的Box授權代碼

authorize_url = 'https://www.box.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID' 

    def myRequest(path, method=None, options=None): 
     response = requests.request(method, path, **options) 
     return json.dumps(response.json()) 

    code = myRequest(
     path=authorize_url, 
     method='GET', 
     options={ 
      'headers':{ 
       'response_type': 'code', 
       'client_id': CLIENT_ID, 
       } 
     } 
    ) 
+0

你能告訴我們你現在的代碼是什麼嗎?所以我們可以幫你解決它。 –

+0

def request(path,method = None,options = None): response = requests.request(method,path,** options) return json.dumps(response.json()) – ray

+0

code = requests( path = authorize_url, 方法= 'GET', 選項= { '頭':{ 'RESPONSE_TYPE': '代碼', 'CLIENT_ID':CLIENT_ID, }} ) – ray

回答

2

您必須使用模塊來發出HTTP請求,例如requestsurllib並按照指示here的說明進行操作。

我有一個非常簡單的應用程序瓶這裏設置:https://github.com/seanrose/box-oauth2-example

的「代碼」發送用戶Box的授權URL後返回到您的應用程序。

+0

這是正確的方法是什麼?我用這個來得到令牌,這是正確的。但我已經嘗試了一整天,無法獲得代碼,對不起... def request(path,method = None,options = None): response = requests.request(method,path,** options) return json.dumps(response.json()) 代碼=請求( 路徑= authorize_url, 方法= 'GET', 選項= { '報頭':{ 'RESPONSE_TYPE': '代碼', 「CLIENT_ID ':CLIENT_ID, } } ) – ray

+0

我所做的是首先打印一個URL,用戶打開它,直到點擊允許,我得到代碼,然後使用代碼獲取令牌,最後使用令牌訪問用戶'的內容。但現在我無法獲得代碼。如果我手動設置代碼,一切工作正常。 – ray