2017-01-04 6 views
0

Firebaseリソースを取得してアクセストークンを渡そうとすると、常に401エラーが発生します。私は何が間違っているのか分かりません。Firebase(REST API/python)の呼び出しでServiceAccountCredentialsを使用して認証できません

私はthis manualに従います:サービスアカウントを作成した後、オブジェクトを作成し、範囲https://www.googleapis.com/auth/firebaseを渡します。次に、作成したオブジェクトからトークンを取得し、docsに記載されているように、access_tokenクエリパラメータとして渡し、Firebase DBからデータを取得しようとします。しかし、私は401エラー(無許可)を取得し続けます。ところで、トークンは、jsonwebtokenのようなツールをチェックインすると無効に見えます(おそらく私はそこに何か間違っている/間違った秘密を提供していますか?)。

だから、私のコードは次のようである:

import httplib2 
from oauth2client.service_account import ServiceAccountCredentials 

credentials = ServiceAccountCredentials.from_json_keyfile_name('/path/auth.json', scopes=['https://www.googleapis.com/auth/firebase']) 
token = credentials.get_access_token().access_token 
http = httplib2.Http() 
result = http.request('https://my-project.firebaseio.com/srv.json?access_token=%s' % token) 

、結果体は常に401 HTTPステータスコードと一緒に{"error" : "Unauthorized request."}です。

答えて

3

あなたのスコープは十分ではないと思います。ここで私は資格情報とcredentials.authorize HTTPラッパーを使用して作業を終えている終わりの例です。

from oauth2client.service_account import ServiceAccountCredentials 
from httplib2 import Http 
import json 

_BASE_URL = 'https://my-app-id.firebaseio.com' 
_SCOPES = [ 
    'https://www.googleapis.com/auth/userinfo.email', 
    'https://www.googleapis.com/auth/firebase.database' 
] 

# Get the credentials to make an authorized call to firebase  
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    _KEY_FILE_PATH, scopes=_SCOPES) 

# Wrap the http in the credentials. All subsequent calls are authenticated 
http_auth = credentials.authorize(Http()) 

def post_object(path, objectToSave): 
    url = _BASE_URL + path 

    resp, content = http_auth.request(
     uri=url, 
     method='POST', 
     headers={'Content-Type': 'application/json'}, 
     body=json.dumps(objectToSave), 
) 

    return content 

objectToPost = { 
    'title': "title", 
    'message': "alert" 
} 

print post_object('/path/to/write/to.json', objectToPost) 
+0

ありがとうございました!私は問題の原因が間違ったスコープであると推測できませんでした。 – Serge

関連する問題