This is a migrated thread and some comments may be shown as answers.

Authenticate google API requests with plugins.googleplus.login

0 Answers 130 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Patryk
Top achievements
Rank 1
Patryk asked on 04 May 2018, 01:20 PM
Hello there,
I am trying to make hybrid Cordova app for android which shall read/write cells in google spreadsheets (also create sheets etc.).
I am using cordova-plugin-googleplus, set up the project according to instructions, and login/logout etc works well.
When I set spreadsheet settings to public:
I can read from spreadsheet
I cannot write to spreadsheet due to error:
Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
I've searched the internet and could not manage to make it work, here is my current code:
At start I am loading google API:
<script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};handleClientLoad()" onreadystatechange="if (this.readyState === 'complete') this.onload()"> </script>
which triggers:
function handleClientLoad() { gapi.load('client', initClient); } function initClient() { gapi.client.init( {apiKey:API_KEY, clientId:CLIENT_ID, discoveryDocs:DISCOVERY_DOCS, scope:SCOPES} ) }
tried also:
function initClient() // Initializes the API client library and sets up sign-in state listeners. { gapi.client.load("sheets", "v4", function(response){console.log(response);}); gapi.client.setApiKey(API_KEY); }
with the same result.
Then I am login in using plugins.googleplus.login:
function login() { window.plugins.googleplus.login ( { 'webClientId' : CLIENT_ID, 'scope' : 'https://www.googleapis.com/auth/spreadsheets', 'offline': false }, function(obj) { gapi.client.setToken(obj.accessToken); if(!firebase.auth().currentUser) { firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(obj.idToken)).then ( function(success){console.log(success);} ) .catch ( function(error){console.log(error);} ); } else { console.log("Login error: Already signed in database!"); } }, function(msg){console.log(msg);} ); }
Where:
var DISCOVERY_DOCS = ["https://sheets.googleapis.com/$discovery/rest?version=v4"]; // Array of API discovery doc URLs for APIs used by the quickstart var SCOPES = "https://www.googleapis.com/auth/spreadsheets"; // Authorization scopes required by the API; multiple scopes can be included, separated by spaces. var API_KEY = "uniqueWebApiKey"; var CLIENT_ID = "uniqueWebClientId";
What can I use to make my gapi client use authentication from plugins.googleplus.login?
gapi.client.setToken(obj.accessToken);
does not seem to do the job.
I would appreciate any tips.
Tags
Apache Cordova
Asked by
Patryk
Top achievements
Rank 1
Share this question
or