Created
December 7, 2012 14:47
-
-
Save sallespromanager/4233701 to your computer and use it in GitHub Desktop.
oauth2 client to Oauth2orize server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
| <title>OAuth 2 User Agent Authentication Flow Demo</title> | |
| <script type="text/javascript" charset="utf-8" src="js/bootstrap/jquery.js"></script> | |
| <script type="text/javascript" charset="utf-8"> | |
| $(function () { | |
| var extractToken = function(hash) { | |
| var match = hash.match(/access_token=(\w+)/); | |
| return !!match && match[1]; | |
| }; | |
| var setting = | |
| { | |
| 'host': "localhost:3000" | |
| , 'clientId': "abc123" | |
| }; | |
| var authHost = "http://" + setting.host; | |
| var resourceHost = "http://" + setting.host; | |
| var endUserAuthorizationEndpoint = authHost + "/dialog/authorize"; | |
| $('div.authenticate').show(); | |
| var authUrl = endUserAuthorizationEndpoint + | |
| "?response_type=code" + | |
| "&client_id=" + setting.clientId + | |
| "&redirect_uri=" + "localhost/oa.html"; | |
| $("a.connect").attr("href", authUrl); | |
| var extractCode = function(hash) { | |
| var match = hash.match(/code=(\w+)/); | |
| return !!match && match[1]; | |
| }; | |
| var code = extractCode(document.location.href); | |
| if (code) { | |
| $('div.authenticated').show(); | |
| $('span.code').text(code); | |
| $.ajax({ | |
| url: resourceHost + '/oauth/token', | |
| type: "POST", | |
| data: { grant_type : "authorization_code", | |
| code : code, | |
| redirect_uri : "localhost/oa.html" } | |
| , beforeSend: function (xhr) { | |
| xhr.setRequestHeader('Access-Control-Allow-Origin', "localhost"); | |
| xhr.setRequestHeader('Accept', "application/x-www-form-urlencoded"); | |
| } | |
| , success: function (response) { | |
| console.log(response); | |
| var token = extractToken(document.location.hash); | |
| if (token) { | |
| $('div.authenticated').show(); | |
| $('span.token').text(token); | |
| $.ajax({ | |
| url: resourceHost + '/api/userinfo' | |
| , beforeSend: function (xhr) { | |
| xhr.setRequestHeader('Authorization', "OAuth " + token); | |
| xhr.setRequestHeader('Accept', "application/json"); | |
| } | |
| , success: function (response) { | |
| var container = $('span.user'); | |
| if (response) { | |
| container.text(response.user.info); | |
| } else { | |
| container.text("An error occurred."); | |
| } | |
| } | |
| }); | |
| }; | |
| } }); | |
| }; | |
| }); | |
| </script> | |
| <style> | |
| .hidden { | |
| display: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="authenticate hidden"> | |
| <a class="connect" href="">Connect</a> | |
| </div> | |
| <div class="authenticated hidden"> | |
| <p> | |
| You are using code | |
| <span class="code">[no code]</span>. | |
| </p> | |
| <p> | |
| You are using token | |
| <span class="token">[no token]</span>. | |
| </p> | |
| <p> | |
| Your SoundCloud username is | |
| <span class="user">[no username]</span>. | |
| </p> | |
| </div> | |
| </body> | |
| </html> |
From http://stackoverflow.com/questions/29934606/how-to-get-access-token-from-the-url 👍
var url = 'http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600',
access_token = url.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me extractToken functions doesnot work with JWT tokens containing DOTS!