Skip to content

Instantly share code, notes, and snippets.

@lilmuckers
Last active December 15, 2015 22:09
Show Gist options
  • Save lilmuckers/5331280 to your computer and use it in GitHub Desktop.
Save lilmuckers/5331280 to your computer and use it in GitHub Desktop.
A browser based javascript app to get an oauth access_token from GitHub via the webpage, with added google analytics.
jQuery(function($){
//github API url
var ghUrl = 'https://api.github.com/authorizations'
//the form - the response is in a form because... reasons...
var form = $('#ghAuthApi')
var responseForm = $('#ghAuthApiResponse')
//a function to do our field mapping
var reformat = function(array){
var obj = {};
for(i=0; i<array.length; i++){
var a = array[i]
var name = a.name
var value = a.value
//if there's no value - continue
if(!value){
continue
}
//if this is a multiple party
if(obj[name]){
if(typeof obj[name] != 'object'){
var hold = obj[name]
obj[name] = []
obj[name].push(hold)
}
obj[name].push(value)
} else {
obj[name] = value
}
}
return obj
}
form.submit(function(){
//serialize the form data the way we like
var userData = reformat(form.serializeArray())
//do a google analytics event
if(typeof _gaq != 'undefined'){
_gaq.push(['_trackEvent', 'BlogScript', 'submit', 'oAuth Token'])
}
//perform the ajax request to github
$.ajax(ghUrl, {
type: 'POST',
crossDomain: true,
data: JSON.stringify({
note: userData.note,
scopes: userData.scopes
}),
beforeSend: function(xhr){
xhr.setRequestHeader("Authorization",
"Basic "+btoa(userData.username+':'+userData.password)+'=')
},
success: function(data, status, response){
//hide the alert messages
responseForm.find('.alert').hide()
//populate the information in the success
$('#oauthToken').html(data.token)
$('#oauthNote').html(data.note)
var date = new Date(data.updated_at)
$('#oauthCreated').html(date.toDateString()+' at '+date.toLocaleTimeString())
$('#oauthScopes').html(data.scopes.join(', '))
responseForm.find('.alert-success').fadeIn()
//do a google analytics event
if(typeof _gaq != 'undefined'){
_gaq.push(['_trackEvent', 'BlogScript', 'success', 'oAuth Token'])
}
},
error: function(response, status, error){
//hide the alert messages
responseForm.find('.alert').hide()
if(response.responseText.length > 0){
var data = JSON.parse(response.responseText)
if(data.message){
responseForm.find('.alert-error p').html('Error communicating with GitHub: '+data.message)
responseForm.find('.alert-error').fadeIn()
//do a google analytics event
if(typeof _gaq != 'undefined'){
_gaq.push(['_trackEvent', 'BlogScript', 'error', 'oAuth Token: '+data.message])
}
return
}
}
responseForm.find('.alert-error p').html('Error communicating with GitHub: '+error)
responseForm.find('.alert-error').fadeIn()
//do a google analytics event
if(typeof _gaq != 'undefined'){
_gaq.push(['_trackEvent', 'BlogScript', 'error', 'oAuth Token: '+error])
}
}
})
return false
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment