【问题标题】:Access token for Google AuthenticationGoogle 身份验证的访问令牌
【发布时间】:2017-04-29 09:05:53
【问题描述】:
我有一个使用 Google 身份验证的 ASP.NET MVC Web 应用程序。身份验证部分工作正常。但我正在尝试找出如何在我的代码中获取用于登录的访问令牌,以便我可以在我拥有并希望使用的 Web api 的标头中传递它。
【问题讨论】:
标签:
c#
asp.net-mvc
google-authentication
【解决方案1】:
以下是使用Google's Sign-In button 模板初始化登录和授予权限的方法:
<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;
}
然后您可以将 idToken (accessToken) 传递给 API。