2017-04-26 16 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のスキーマにアクセスすることができる午前:次のように

は、私は基本的な認証を行うことができますよ。アクセスしようとし

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

しかし、私はエラーを取得する(カール経由で正常に動作する)(rest_framework.tokenauthenticaitonを経由して)私のトークン認証を使用したいのですが、私のコードは次のようになりますスキーマは、私が手:

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

ドキュメントはTokenAuthenticationスキーマを必要とし、引数としてトークンを示し、しかし例はJWTと、ないdjangos rest_framework.tokenauthenticationで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) 
関連する問題