2017-04-26 34 views
1

我使用django的rest_framework庫爲django應用程序構建了一個API。一切都很順利,我可以像預期的那樣通過curl命令訪問我的API。在CoreAPI中使用TokenAuthentication時遇到問題

現在,我想使用CoreAPI以客戶端庫的形式使事情更加優雅。

我能夠做到基本身份驗證類似如下:

auth = coreapi.auth.BasicAuthentication(username=user, password=password) 
client = coreapi.Client(auth=auth) 

,我能夠訪問API的模式就好了。

不過,我想用我的令牌認證(通過rest_framework.tokenauthenticaiton)(其通過捲曲優秀作品)我得到一個錯誤,我的代碼看起來是這樣的:

token = 'Token abc12345' 
#tried the following: 
#token = 'abc12345' 
#token = 'Authorization: Token abc12345' 
auth = coreapi.auth.TokenAuthentication(token=token) 
client = coreapi.Client(auth=auth) 

試圖訪問架構,我得到:

coreapi.exceptions.ErrorMessage: <Error: 401 UNAUTHORIZED> 
    detail: "Authentication credentials were not provided." 

文檔顯示TokenAuthentication需要的模式和令牌作爲參數,但是示例顯示TokenAuthentication與智威湯遜,不與Django的rest_framework.tokenauthentication。

任何意見將不勝感激!

回答

3

我剛剛有同樣的問題。修正是爲coreapi.auth.TokenAuthentication設置一個「scheme ='Token'」參數。所以,像這樣的東西可能適合你:

token = 'abc12345' # don't put the word 'Token' in front. 
auth = coreapi.auth.TokenAuthentication(scheme='Token', token=token) 
client = coreapi.Client(auth=auth)