简体   繁体   English

验证服务帐户以使用JavaScript客户端库调用Google API

[英]authenticating a service account to call a Google API with JavaScript client library

I want to make JSON-RPC calls from localhost (WAMP environment) to the Google FusionTables API (and a couple of other APIs) using the Google Client Library for JavaScript 我想使用Google Client Library for JavaScript从localhost(WAMP环境)到Google FusionTables API (以及其他一些API)进行JSON-RPC调用

Steps I have taken: 我采取的步骤:

  1. setup a project on the Google Developer Console Google Developer Console上设置项目
  2. enabled the FusionTables API 启用了FusionTables API
  3. created a service account and downloaded the JSON file. 创建了一个服务帐户并下载了JSON文件。
  4. successfully loaded the JS client library with the auth package: gapi.load('client:auth2', initAuth); 使用auth包成功加载了JS客户端库: gapi.load('client:auth2', initAuth);
  5. constructed the init method parameter the following 3 ways: 构造init方法参数有以下3种方式:
    • the downloaded JSON verbatim 下载的JSON逐字
    • the downloaded JSON modified to include the scope 下载的JSON已修改为包含范围
    • just the client ID and scope 只是客户端ID和范围
  6. tried (and failed) to initialize the GoogleAuth instance: gapi.auth2.init(params) 尝试(并失败)初始化GoogleAuth实例: gapi.auth2.init(params)
function failed(reason) {
        console.log(reason);
    }
    gapi.load('client:auth2', initAuth);

    function initAuth() {

        var APIkey = 'MY API KEY';
        gapi.client.setApiKey(APIkey); //I understand this to be unnecessary with authorized requests, included just for good measure

        var GDTSAKey = 'MY SERVICE ACCOUNT KEY';
        var scopes = 'https://www.googleapis.com/auth/fusiontables';
        gapi.auth2.init({
            client_id: "101397488004556049686",
            scope: 'https://www.googleapis.com/auth/fusiontables'
        }).then(signin, failed("couldn't initiate"));
        //passing the downlaoded JSON object verbatim as parameter to init didn't work either
    } //initAuth()

    function signin() {
        gapi.auth2.getAuthInstance().signIn().then(makeAPIcall), failed("couldn't sign-in");
    }

    function makeAPIcall(){
        gapi.client.load('fusiontables', 'v2', function(){
            var tableId = '1PSI_...';
            var table = gapi.client.fusiontables.table.get(tableId);
            document.querySelector("#result").innerHTML = table;            
        });
    }

based on JS client library >> Samples 基于JS客户端库>> Samples

the gapi.auth2.init method invokes the second callback (which I understand to be an error handler): failed("couldn't initiate"), but then, curiously, I also get `couldn't sign in' which could only have originated from within the provided success handler. gapi.auth2.init方法调用第二个回调(我理解为错误处理程序):失败(“无法启动”),但奇怪的是,我也得到“无法登录”这只能源自提供的成功处理程序。 What's going on? 这是怎么回事? How do I get this to work? 我如何让它工作?

Note: I am only willing to try the CORS/xhr, if there is no way to do it with JS client lib. 注意:我只愿意尝试CORS / xhr,如果没有办法用JS客户端lib做的话。

What's going on? 这是怎么回事?

You are trying to use a service account with the Google JavaScript client library which does not support service accounts. 您正在尝试将服务帐户与不支持服务帐户的Google JavaScript客户端库一起使用。

How do I get this to work? 我如何让它工作?

Switch to Oauth2 authentication or if you must use a service account switch to a server sided language like PHP or python for example. 切换到Oauth2身份验证,或者如果必须使用服务帐户切换到服务器端语言,例如PHP或python。 Which support service account authentication. 哪个支持服务帐户认证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 nodejs 库为谷歌经销商 api 验证服务帐户时出现问题 - Problems authenticating Service Account for google reseller api using the nodejs library 将 Google 服务帐户与 google API JavaScript 客户端一起使用 - Use Google Service Account with google API JavaScript Client 使用Javascript客户端库作为Angular 2服务的Google API端点 - Google API Endpoints using Javascript client library as an Angular 2 Service JavaScript中的Google API客户端库无法在本地运行 - Google API client library in javascript not working locally 使用JavaScript的Google API客户端库访问Google Sites API - Using Google APIs Client Library for JavaScript to access Google Sites API 验证一个帐户的Google Analytics Core Reporting API - Authenticating to the Google Analytics Core Reporting API for One Account 适用于JavaScript的Google API客户端库 - 404方法调用 - Google APIs Client Library for JavaScript - 404 on method call 如何在没有客户端库但仅使用 HTTP Post 的情况下验证为 Google 服务帐户? - How to authenticate as Google service account without a client library but only with HTTP Post? 如何将Google API Javascript客户端库加载到Chrome应用中 - How to Load Google API Javascript Client Library into Chrome App 将Google API Javascript客户端库加载到Chrome扩展程序中 - Loading Google API Javascript Client Library into Chrome Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM