Hello,
I am working on a MEAN app that uses Auth0 for user authentication.
I would now like to utilize some Google APIs in the app.
The app will eventually have automated features that run when the user is offline, so I believe I will need to grantOfflineAccess to the app.
When the user grants authorization, I would prefer to redirect to the OAuth2 consent page, and then grantOfflineAccess once the user has signed in. Is it possible to use the signIn method, and then call grantOfflineAccess afterwards, silently?
If not, I'm willing to use a popup and just use grantOfflineAccess, but whenever I do so, I get the error popup_blocked_by_browser. I'm using a button to trigger the grantOfflineAccess().
Any insight or help on getting either of these flows to work would be greatly appreciated.
Thanks,
Blaine H.
Hi @bxboxer219, just to clarify: are you saying that your server will perform some tasks communicating to the Google APIs while the user is not on the app?
Please provide a snippet of code calling grantOfflineAccess. If triggered synchronously after a user click, it should not be blocked by the browser.
Really strange, but today I also came up with this problem. Here is my piece of code:
this.loginWithGoogle = function () {
var auth_user = $window
.gapi
.auth2
.getAuthInstance();
auth_user
.signIn()
.then(function (user) {
auth_user.grantOfflineAccess().then(function (code) {
// sending basic user data + authorization code to backend
// Logging user in the app
})
});
};
Is there a way to make signIn() and grantOfflineAccess() with the same auth instance? Or, better solution, is it possible that server authorization code is sent in the response of the first request (signIn())? If yes, how?
@mightymatth you can use gapi.auth2.authorize and request 'permission code' as response_type.
@TMSCH authorize cannot be used once a web app uses the init + signIn path. Is there a way to use both? I'm facing the exact same issue. The grantOfflineAccess call fails with a popup_blocked_by_browser.
You should not trigger the call to grantOfflineAccess after the resolution of signIn. A call that opens a popup should be made synchronously after a user interaction, so you should add a button for the user to click on and trigger the call to grantOfflineAccess on click.
gapi.auth2.authorize and init/signIn have different internal mechanisms and can't be used together.
Original Post is 3 years old but below code is working for me. Hope it helps someone.
handleLoginClick = () => {
const auth2 = gapi.auth2.getAuthInstance();
auth2.signIn().then(user => {
auth2.grantOfflineAccess()
.then((res) => {
console.log(res);
this.signup(user); // sending code and profile to backend .
});
});
};
Most helpful comment
Really strange, but today I also came up with this problem. Here is my piece of code:
Is there a way to make
signIn()andgrantOfflineAccess()with the same auth instance? Or, better solution, is it possible that server authorization code is sent in the response of the first request (signIn())? If yes, how?