尝试使用库后,我正尝试自己实施Google OAuth登录。
我在Google上创建了应用,并尝试使用代码登录:
查看:
<a href="#" class="button" id="A1" onclick="OpenGoogleLoginPopup();" name="butrequest"> <span>Login with google</span></a>
...
<script type="text/javascript" language=javascript>
function OpenGoogleLoginPopup() {
var url = "https://accounts.google.com/o/oauth2/auth?";
url += "scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email&";
url += "state=%2Fprofile&"
url += "redirect_uri=<%=Return_url %>&"
url += "response_type=token&"
url += "client_id=<%=Client_ID %>";
window.location = url;
}
</script>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Client_ID = ConfigurationSettings.AppSettings["google_clientId"].ToString();
Return_url = ConfigurationSettings.AppSettings["google_RedirectUrl"].ToString();
}
if (Request.QueryString["access_token"] != null)
{
String URI = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Request.QueryString["access_token"].ToString();
WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(URI);
string b;
using (StreamReader br = new StreamReader(stream))
{
b = br.ReadToEnd();
}
...
但是,在我登录谷歌并从谷歌Request.QueryString["access_token"]
重定向到此页面后,这是空的。
我现在做错了什么?我使用谷歌控制台标有蓝色箭头的数据:
答案 0 :(得分:2)
在Google首次重定向时,不会向您返回access_token。
OAuth2的工作原理如下:
我建议您通读https://developers.google.com/accounts/docs/OAuth2Login#authenticatingtheuser