Unable to access BigQuery from local App Engine development server

后端 未结 5 1339
灰色年华
灰色年华 2020-11-27 15:00

This is specifically a question relating to server to server authorisation between a python Google AppEngine app and Google\'s BigQuery, but could be relevant for other clou

5条回答
  •  粉色の甜心
    2020-11-27 15:32

    Is it possible to get the App Engine local development server to authenticate with the remote BigQuery service?

    I think it's impossible to use AppAssertionCredentials as authentication method between BigQuery service and your local App Engine server currently.

    Alternatively, I'm using OAuth2 authentication which is associated with specific user(this user must be registered in your project at google api console) to access BigQuery from local App Engine server.

    For getting user OAuth2 authentication, I use oauth2client.client module in the app code.

    I hope this will be helpful to your problem.

    Updated:

    This is what I'm doing for getting the user OAuth2 authorization.

    Edited:

    Added missing import statement. Thanks mattes!

    import os
    import webapp2
    import httplib2
    from oauth2client.client import OAuth2Credentials
    from oauth2client.appengine import StorageByKeyName, CredentialsModel, OAuth2DecoratorFromClientSecrets
    from google.appengine.api import users
    
    oauth2_decorator = OAuth2DecoratorFromClientSecrets(
        os.path.join(os.path.dirname(__file__), 'client_secrets.json'),
        scope='https://www.googleapis.com/auth/bigquery')
    oauth2_decorator._kwargs = {'approval_prompt': 'force'}
    
    
    class TestPage(webapp2.RequestHandler):
      @oauth2_decorator.oauth_required
      def get(self):
        user_id = users.get_current_user().user_id()
        credentials = StorageByKeyName(CredentialsModel, user_id, 'credentials').locked_get()
        http = credentials.authorize(httplib2.Http()) # now you can use this http object to access BigQuery service
    
    
    application = webapp2.WSGIApplication([
      ('/', TestPage),
      (oauth2_decorator.callback_path, oauth2_decorator.callback_handler()),
    ], debug=True)
    

提交回复
热议问题