gapi.load('client:auth2', initAuth);
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; }); }
基于JS client library >> Samples
gapi.auth2.init
方法调用第二个回调(我理解为错误处理程序):失败(“无法启动”),但奇怪的是,我也得到“无法登录”这只能来自提供的成功处理程序。这是怎么回事?我如何让它工作?
注意:我只愿意尝试使用CORS / xhr,如果用JS客户端lib无法做到这一点。
答案 0 :(得分:4)
发生了什么事?
您正尝试将服务帐户与不支持服务帐户的Google JavaScript客户端库一起使用。
如何让它发挥作用?
切换到Oauth2身份验证,或者如果必须使用服务帐户切换到服务器端语言,例如PHP或python。哪个支持服务帐户认证。