Created
June 21, 2017 23:32
-
-
Save nojaja/5c03e2e77c6e1e792de58f76fcf30a73 to your computer and use it in GitHub Desktop.
gist Access
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
| <button onclick="gitlogin()">GIT Login</button> | |
| <button onclick="CreateGist()">Create Gist</button> |
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
| 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); | |
| }); | |
| } |
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
| <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