Github jsonp api source code

Does GitHub have jsonp api for file source? I know what BitBucket has, but I cannot find any information for GitHubs (if they have).

Is not it? If not, then bummer ...

+7
source share
4 answers

There is an API for retrieving data content from github. This is part of the v3 github API .

You make a request

https://api.github.com/repos/{username}/{repository name}/contents/{filepath and name} 

eg. https://api.github.com/repos/mono/monodevelop/contents/README

If you do not set the accepts header, you will get back some JSON with the contents of the file encoded in base64. You will have to decode this, which is very easy in node.js, but more pain in the browser. You can easily find base64 decoders in javascript in other questions on stackoverflow. One note: the base64 code you return from github has newline characters in it to make it formatted, and many base64 decoders cannot handle new lines, so you may need to delete them or change the decoder.

You probably just need content and you don't need other material in json (e.g. sha and length, etc.), so you can make your life easier by setting the Accept header to application/vnd.github.3.raw .

Here's an example with the accepts header using curl:

 curl -i https://api.github.com/repos/mono/monodevelop/contents/README --header "Accept: application/vnd.github.3.raw" 

Now, if you are using node or curl, perhaps this is good, but if you are working in a browser, you will need to use CORS for this. Github only allows access from hosts registered with OAuth Applications. This is not particularly difficult to do, but for my usecase (bookmarketlet) this is not an option.

There is a way to access without using CORS, and this is using JSONP, you can add, for example. ?callback=_processGithubResponse to get a javascript output suitable for inclusion with a script tag (which calls a function named _processGithubResponse with the response). Unfortunately, you cannot set the accepts header, so in this case you are stuck with the decoding base64.

If you are using node.js, I would recommend you use node-github , which makes using the API easier.

+9
source

A specified in the GitHub API documentation, any call supports JSONP activation mode:

You can send the ?callback Callback parameter to any GET call to get the results wrapped in a JSON function. This is most commonly used when browsers want to embed GitHub content in web pages, bypassing cross domain issues. The response includes the same data output as the regular API, as well as the corresponding HTTP header information.

If you want to get the current source of the file (or any version of it), you need to either know the SHA Blob in which it is stored. See the Git Database API for more information on this topic.

Most often, no one knows SHA, but only the relative path to the file in the working directory.

Then you will need to follow these steps

Select a commit command from the list

Get the tree it points to and recursively list each entry (Trees and Blobs)

Find your Blob that matches the path found, find out its SHA and apply the first process

+3
source

I don't think GitHub has an API to extract the source of a file. They have jsonp callbacks though, by specifying callback = funciton after any API call (i.e. curl https://api.github.com?callback=foo )

Perhaps you can use the Trees API to find the source files available in the repository with a certain commit (HEAD of the main branch). Then you can simply purchase the source using the URL for the original version of the file (i.e. https://raw.github.com/robbyrussell/oh-my-zsh/master/lib/directories.zsh )

0
source

Kybernetikos has a great answer, but if you want a faster and easier setup, you can also try RawGit https://rawgit.com/

It serves for raw files directly from GitHub with the corresponding Content-Type headers. Just paste in your file or gist url and it gives you API URLs for production and development.

0
source

All Articles