Skip to content

Instantly share code, notes, and snippets.

@nojaja
Created June 21, 2017 23:32
Show Gist options
  • Select an option

  • Save nojaja/5c03e2e77c6e1e792de58f76fcf30a73 to your computer and use it in GitHub Desktop.

Select an option

Save nojaja/5c03e2e77c6e1e792de58f76fcf30a73 to your computer and use it in GitHub Desktop.
gist Access
<button onclick="gitlogin()">GIT Login</button>
<button onclick="CreateGist()">Create Gist</button>
var token="";
/*
Assuming jQuery Ajax instead of vanilla XHR
*/
function gitlogin() {
//Get Github Authorization Token with proper scope, print to console
$.ajax({
url: 'https://api.github.com/authorizations',
type: 'POST',
beforeSend: function(xhr) {
var name=prompt("GIT Login USERNAME","");
var pass=prompt("GIT Login PASSWORD","");
xhr.setRequestHeader("Authorization", "Basic " + btoa(name+":"+pass));
},
data: '{"scopes":["gist"],"note":"ajax gist test for a user"}'
}).done(function(response) {
console.log(response);
token=response.token;
});
}
function CreateGist() {
//Create a Gist with token from above
$.ajax({
url: 'https://api.github.com/gists',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization","token "+token);
},
data: '{"description": "a gist for a user with token api call via ajax","public": true,"files": {"file1.txt": {"content": "String file contents via ajax"}}}'
}).done(function(response) {
console.log(response);
}).fail(function(jqXHR, textStatus, errorThrown){
// エラーの場合処理
console.log(jqXHR,textStatus,errorThrown);
});
}
function PachGist() {
//Using Gist ID from the response above, we edit the Gist with Ajax PATCH request
$.ajax({
url: 'https://api.github.com/gists/GIST-ID-FROM-PREVIOUS-CALL',
type: 'PATCH',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "token TOKEN-FROM-AUTHORIZATION-CALL");
},
data: '{"description": "updated gist via ajax","public": true,"files": {"file1.txt": {"content": "updated String file contents via ajax"}}}'
}).done(function(response) {
console.log(response);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment