【问题标题】:OAuth 2.0 for Server to Server Applications using JavaScript使用 JavaScript 的服务器到服务器应用程序的 OAuth 2.0
【发布时间】:2012-08-14 10:35:08
【问题描述】:

我已经使用 Google-bigquery 和 JavaScript 工作了一段时间,在获得一些帮助 here 之后,我意识到您需要与项目相关联的 Google 登录详细信息才能授权并实现您的尝试去做。

我想要达到的目标:- 允许用户访问我的页面,并查看数据。比如我可能会根据天气预报显示一些公共数据,所以我不需要任何用户认证,

目前用于研究和开发,我正在使用 OAuth 2.0 用于 Web 服务器应用程序,我想摆脱这个,因为我们不需要任何用户数据,除了拥有我的项目客户 ID 电子邮件 ID 等...

我在 OAuth 2.0 for Server to Server Applications 上阅读过,似乎没有任何对 JavaScript 的支持,因此最终用户不必参与其中。

是否有任何解决方案或安全的快速修复,我已尝试从 sample 更改配置代码以查看会发生什么但没有运气 -

var config = {
            'client_id' : 'xxxxxxxxxxx.apps.googleusercontent.com',
            "iss" : "xxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com",
            "scope" : "https://www.googleapis.com/auth/bigquery",
            "aud" : "https://accounts.google.com/o/oauth2/token",
            "exp" : 1328554385,
            "iat" : 1328550785
        };

我在这里缺少什么。

在此先感谢您的帮助和建议,我为此苦苦挣扎了很长时间。

【问题讨论】:

    标签: javascript google-api google-bigquery


    【解决方案1】:

    由于无法在客户端 JavaScript 代码中隐藏客户端密码,因此无法授权客户端 JavaScript 应用通过服务器到服务器 OAuth 流使用 BigQuery。

    在这种情况下,唯一的解决方案是对来自 JavaScript 应用程序的 API 调用使用服务器端代理。下面是如何通过 AppEngine 代理查询调用的 sn-p(注意:下面的代码对任何用户开放,它会进行任何检查以确保调用正在通过您的特定 JavaScript 客户端运行)。

    import httplib2
    
    from apiclient.discovery import build
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    from oauth2client.appengine import AppAssertionCredentials
    
    # BigQuery API Settings
    SCOPE = 'https://www.googleapis.com/auth/bigquery'
    PROJECT_ID = 'XXXXXXXXXX' # REPLACE WITH YOUR Project ID
    
    # Create a new API service for interacting with BigQuery
    credentials = AppAssertionCredentials(scope=SCOPE)
    http = credentials.authorize(httplib2.Http())
    bigquery_service = build('bigquery', 'v2', http=http)
    
    
    class StartQueryHandler(webapp.RequestHandler):
      def post(self):
        query_string = self.request.get('query')
        jobCollection = bigquery_service.jobs()
        jobData = {
          'configuration': {
            'query': {
              'query': query_string,
            }
          }
        }
        try:
          insertResponse = jobCollection.insert(projectId=PROJECT_ID,
                                                body=jobData).execute()
          self.response.headers.add_header('content-type',
                                           'application/json',
                                           charset='utf-8')
          self.response.out.write(insertResponse)
        except:
          self.response.out.write('Error connecting to the BigQuery API')
    
    
    class CheckQueryHandler(webapp.RequestHandler):
      def get(self, job_id):
        query_job = bigquery_service.jobs()
        try:
          queryReply = query_job.getQueryResults(projectId=PROJECT_ID,
                                                 jobId=job_id).execute()
          self.response.headers.add_header('content-type',
                                           'application/json',
                                           charset='utf-8')
          self.response.out.write(queryReply)
        except:
          self.response.out.write('Error connecting to the BigQuery API')
    
    
    application = webapp.WSGIApplication(
                                         [('/startquery(.*)', StartQueryHandler),
                                         ('/checkquery/(.*)', CheckQueryHandler)],
                                         debug=True)
    
    def main():
      run_wsgi_app(application)
    
    if __name__ == "__main__":
      main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2015-09-28
      • 2016-06-12
      • 2013-08-26
      • 1970-01-01
      相关资源
      最近更新 更多