How to set SCOPE for loginwithGooglePLus

Hi
Please could somebody help explain how to set the user SCOPE when using Backendless.UserService.loginWithGooglePlus ?
I need to access the Google Drive API once authorised, but I think the lack of scope in the login with google plus api is causing a 401 response.
My function from my app is :
function loginBackendlessWithGoogle() {
Backendless.UserService.loginWithGooglePlus()
.then( userLoggedInWithGoogle ).catch( gotError )
};
Which I then use the access token to do make a XMLHttpRequest to the drive api:
var request = new XMLHttpRequest();
request.open(‘POST’, url);
request.setRequestHeader(‘Authorization’, 'Bearer ’ + userTokenGoogle);
request.setRequestHeader(‘Content-Type’, ‘application/json’);
request.responseType = ‘json’;
request.send();
Thanks
Paul

Hi Paul,

At the moment our API does not accept scopes and thus does not allow using other Google APIs (like Drive API) with the token received.
I’ve created a feature request for this, we’ll notify you in this topic as soon as we have any progress. For reference, the task ID is BKNDLSS-17308.
For now the workaround I can suggest is to manually implement the login with scopes to use it with Google Drive API.

Ah okay ! Thanks.

One question on the workaround - is there a simple way to include the node.js sdk for google into the Backendless cloud code JS ?

I’m not sure if google’s node.js sdk is any different, but for other packages you can npm install --save them and they’ll be deployed along with your business logic code.

Thanks, that has worked

I’ve created my API as suggested above which as mentioned worked. But, it has put my app over the size limit - the app is 26mb, which is some way over even adding the extra packages. And now my app is blocked and will be deleted.

I’m no node.js expert so I don’t know if that size app is expected or if there’s a way to reduce that by limiting modules.

Any suggestions please ?

Looking at a fresh npm install of a backenedless project, with nothing else installed, that is 12.7mb on disk.

Does backendless use this when calculating the size of the app when looking at limits ?

Hi, Paul.
Backendless.UserService.loginWithGooglePlus() is a deprecated method.


You should use loginWithGooglePlusSdk(“accessToken”) instead and pass it the token you received from Google. The login into Google should be done by yourself. And when the exchange process is performing (to receve accessToken) you are free to pass any numbers of scopes.

Sure, I’ll give it a go - but what if I want to use google apis directly ?

you can use it directly, I am not sure that I understand your question. our sdk accepts accessToken from googles sdk. so you can use google apis directly

The original response above said that scopes can’t be passed to with Backendless.UserService.loginWithGooglePlus.

I will try with loginWithGooglePlusSdk. If that does not work, I’d like to continue using the googleapi directly as a service created within a Backendless node.js service as a JS coding in business logic - as per the original recommendation given above. But need to reduce the size - but don’t know how.

You only pass an accessToken to the loginWithGooglePlusSdk (notice the Sdk part in the end) method. And that token you retrieve by using Google’s native APIs, where you can pass any required scopes so that the token would be eligible not only for login, but also for communicating with Google Drive’s native API in your case.
The point is that Backendless.UserService.loginWithGooglePlusSdk method has nothing to do with Google’s scopes, it operates on another level, after you have the accessToken with required scopes.

So the flow is :

  • Create an OAUth2 client thru google Auth ApI
  • Get the access token along with the Scope thru google API
  • Pass the accessTOken to loginWIthGooglePlusSDK thru the Backendless API
  • Upload a file thru Google Drive API

Won’t this cause problems with cross site origin requests ? If I’m getting the auth token from one place and then getting the user to login thru Backendless ?

My use case also needs me to manage thru a server to avoid Google CORS issues. We have a chrome plugin that can work on any host url. We record videos, and then want users to be able to upload the video to google drive.

if the above is true, I come back to needing to install a node.js server and will blow the Backendless app size won’t I ?

Is there any documentation for loginWIthGooglePlusSDK ?

Hi Paul

loginWithGooglePlusSdk method accepts three parameters:

  • accessToken - the token you should get somewhere by yourself
  • fieldsMapping - it’s fields mapping between google account properties and Backendless User properties
  • stayLoggedIn - optional, pass TRUE if you want to keep the user logged in

Example:

Backendless.UserService.loginWithGooglePlusSdk(accessToken, fieldsMapping, stayLoggedIn)
    .then(this.onLoggedIn, this.onError);