最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google sign_in: How to use redirect_uri in javascript api - Stack Overflow

programmeradmin2浏览0评论

I'm trying to integrate google sign in my website, and i'm using firebase for authentication and later I do authorization the user for access their google drives.

and after getting access of their Google drives i want the page to redirect to another page.(localhost:8080/afterlogin), where it will be displayed "WELCOME"

however the user doesn't get to the desired page, I've added the exact redierct_uri in the developer console but no help.

here is my code for initializing the auth object.

gapi.load('auth2', function () {
        // Retrieve the singleton for the GoogleAuth library and set up the client.
        auth2 = gapi.auth2.init({
            client_id: 'MYID.apps.googleusercontent',
            cookiepolicy: 'single_host_origin',
            redirect_uri: 'http://localhost:8080/afterlogin'

            // Request scopes in addition to 'profile' and 'email'
            //scope: 'additional_scope'
        });

i'm using javascript and i read in the docs that javascript api does not use redirect_uris.

However in the docs here

it is stated that we can add redirect_uris in by using gapi.auth2.ClientConfig instead of gapi.auth2.init.

but however, using gapi.auth2.ClientConfig instead of gapi.auth2.init gives me and error. so i added redirect_uri in the latter one as seen in the code above. maybe that is why it is not working?

or i'm doing something wrong in using gapi.auth2.ClientConfig?

any guesses?

I'm trying to integrate google sign in my website, and i'm using firebase for authentication and later I do authorization the user for access their google drives.

and after getting access of their Google drives i want the page to redirect to another page.(localhost:8080/afterlogin), where it will be displayed "WELCOME"

however the user doesn't get to the desired page, I've added the exact redierct_uri in the developer console but no help.

here is my code for initializing the auth object.

gapi.load('auth2', function () {
        // Retrieve the singleton for the GoogleAuth library and set up the client.
        auth2 = gapi.auth2.init({
            client_id: 'MYID.apps.googleusercontent.',
            cookiepolicy: 'single_host_origin',
            redirect_uri: 'http://localhost:8080/afterlogin'

            // Request scopes in addition to 'profile' and 'email'
            //scope: 'additional_scope'
        });

i'm using javascript and i read in the docs that javascript api does not use redirect_uris.

However in the docs here

it is stated that we can add redirect_uris in by using gapi.auth2.ClientConfig instead of gapi.auth2.init.

but however, using gapi.auth2.ClientConfig instead of gapi.auth2.init gives me and error. so i added redirect_uri in the latter one as seen in the code above. maybe that is why it is not working?

or i'm doing something wrong in using gapi.auth2.ClientConfig?

any guesses?

Share Improve this question asked Feb 9, 2018 at 14:27 P.hunterP.hunter 1,3652 gold badges23 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The docs say that you need to override the default redirect_uri using ux_mode='redirect'. Sometimes Google's documentation is a lot more plicated than it needs to be. I presume you are handling the resulting promises from the code you posted. I would try this:

     gapi.load('auth2', function () {
        auth2 = gapi.auth2.init({
            client_id: 'MYID.apps.googleusercontent.',
            cookiepolicy: 'single_host_origin',
            ux_mode: 'redirect',
            redirect_uri: 'http://localhost:8080/afterlogin'

            // Request scopes in addition to 'profile' and 'email'
            //scope: 'additional_scope'
        });

        auth2.signIn().then(function() {
          var profile = auth2.currentUser.get().getBasicProfile();
          console.log('Full Name: ' + profile.getName());
        })

From the docs, under gapi.auth2.ClientConfig

edit - I fixed the issue with the = sign

发布评论

评论列表(0)

  1. 暂无评论