[英]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调用
gapi.load('client:auth2', initAuth);
使用auth包成功加载了JS客户端库: gapi.load('client:auth2', initAuth);
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.