简体   繁体   中英

get access token via javascript

I want to get access token via javascript using my refresh token.So this is my code:

$.ajax({    
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            url: "https://accounts.google.com/o/oauth2/token",
            data: "client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&refresh_token=" + REFRESH_TOKEN + "&grant_type=refresh_token",
            dataType: "json",
            success: function (data) {
                alert(data.d);
            }
        });

But i don't understand google return 400 bad request.Please help me

I'm not sure if what you are doing is valid. Even if it is, it's a bad idea.

The refresh token should only ever live on the server. By having it in available on the client, it can be sniffed and used to compromise the user's security (as if his password was available). That's why there is a good chance that it will never work.

You should implement a short server-side service that stores the refresh token and provides access tokens. Alternatively, just have your javascript request access tokens directly from Google (ie. without bothering with refresh tokens).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM