简体   繁体   中英

Access token for Google Authentication

I have a ASP.NET MVC web application that uses Google Authentication. The authentication part is working fine. But I am trying to find out how I can get hold of the access token for the login in my code so that I can pass it in the header for a web api that I have and wish to consume.

Here's how you can use Google's Sign-In button template to initialize the login & grant of permissions:

<meta name="google-signin-client_id" content="{{ OAUTH2_CLIENT_ID }}">

<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>

<div id="google-signin-button"
     class="g-signin2"
     data-width="170"
     data-height="30"
     data-onsuccess="onSignIn"
     data-onfailure="onSignInFailure">
</div>
function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    var idToken = googleUser.getAuthResponse().id_token;
}

You can then pass the idToken (accessToken) to an API.

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