代码之家  ›  专栏  ›  技术社区  ›  Andrew Stromme

Firebase ApplicationDefaultCredentials在dev_appserver中不工作

  •  0
  • Andrew Stromme  · 技术社区  · 9 年前

    我正在遵循以下说明: https://cloud.google.com/solutions/using-firebase-real-time-events-app-engine

    我正在尝试让我的dev_appserver向firebase数据库发出具有身份的请求。这可以在我部署后工作,但不能在本地工作。

    我跑过 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)
    

    {
      "error" : "Permission denied."
    }
    

    奇怪的是,似乎是firebase出现了问题。我能够使用来自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
    

    我做错了什么?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    感谢@atimothee the essential cue .

    显示所使用的默认作用域 gcloud auth aplication-default login 不包括 userinfo.email firebase.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