0

工作我下面的说明: https://cloud.google.com/solutions/using-firebase-real-time-events-app-engine火力地堡ApplicationDefaultCredentials不dev_appserver

我试图让我的dev_appserver作出资格的请求到一个火力点数据库。这在我部署之后运行,但不在本地运行。

我已经运行gcloud auth application-default login

,并建立了我的凭据,如下所示:

try: 
    from functools import lru_cache 
except ImportError: 
    from functools32 import lru_cache 

import json 

import httplib2 
from oauth2client.client import GoogleCredentials 

_FIREBASE_SCOPES = [ 
    'https://www.googleapis.com/auth/firebase.database', 
    'https://www.googleapis.com/auth/userinfo.email'] 


# Memoize the authorized http, to avoid fetching new access tokens 
@lru_cache() 
def _get_http(): 
    """Provides an authed http object.""" 
    http = httplib2.Http() 
    # Use application default credentials to make the Firebase calls 
    # https://firebase.google.com/docs/reference/rest/database/user-auth 
    creds = GoogleCredentials.get_application_default().create_scoped(
     _FIREBASE_SCOPES) 
    creds.authorize(http) 
    return http 


def firebase_put(path, value=None): 
    """Writes data to Firebase. 
    An HTTP PUT writes an entire object at the given database path. Updates to 
    fields cannot be performed without overwriting the entire object 
    Args: 
     path - the url to the Firebase object to write. 
     value - a json string. 
    """ 
    response, content = _get_http().request(path, method='PUT', body=value) 
    return json.loads(content) 

打电话时firebase_put()我得到

{ 
    "error" : "Permission denied." 
} 

奇怪的是,它只是似乎是火力那是有问题的。我能够使用dev_appserver的ApplicationDefaultCredentials成功发出云语音请求。

我已验证凭据已添加到标题中。

Header { 
    Key: "user-agent" 
    Value: "Python-httplib2/0.9.2 (gzip)" 
} 
Header { 
    Key: "accept-encoding" 
    Value: "gzip, deflate" 
} 
Header { 
    Key: "authorization" 
    Value: "Bearer REDACTED_FOR_PRIVACY" 
} 
Payload: "{\"sender\": \"12314\", \"timestamp\": 1478368765.042335, \"message\": \"asdf\"}" 
FollowRedirects: false 
Deadline: 5 
MustValidateServerCertificate: true 

我在做什么错?

+0

退房这个答案http://stackoverflow.com/a/22723127/2987899 – atimothee

+0

@atimothee,我相信下面这个问题的答案实际上是一样使用默认凭据,对不对?或者是有什么原因,为什么这将工作,而默认凭据不会? – astromme

+1

这个? http://stackoverflow.com/a/40493992/2987899 – atimothee

回答

1

谢谢@atimothee the essential cue

原来gcloud auth aplication-default login使用的默认范围不包括userinfo.emailfirebase.database。包括他们手动修复问题。

gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/firebase.database 
+1

这应该是固定的gcloud 134.它现在请求以下范围:'https://www.googleapis.com/auth/userinfo.email https:// www.googleapis.com/AUTH /云platform' –