c# - Google Oauth - access_token is always null -
after trying utilize libraries, trying implement google oauth login myself.
i have created app on google , i'm trying login code:
view:
<a href="#" class="button" id="a1" onclick="opengoogleloginpopup();" name="butrequest"> <span>login 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>
code behind:
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(); } ...
but after login google , redirect page google request.querystring["access_token"]
null.
what did wrong now? utilize info marked bluish arrow google console:
the access_token not returned on first redirect google.
oauth2 works this:
you send user login url, passing clientid, redirect_uri, , state (you did this) after user logs in successfully, google redirects request redirect_uri , passes state sent , access code. in request server, must post https://accounts.google.com/o/oauth2/token , send them client_secret along code passed you. the response of post contain access_token looking for.i suggest read through https://developers.google.com/accounts/docs/oauth2login#authenticatingtheuser
c# asp.net oauth oauth-2.0 google-oauth
No comments:
Post a Comment