Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub jsonp source code api

Tags:

github

jsonp

Does GitHub have a jsonp api for the source of a file? I know BitBucket has, but I can't find any information for GitHubs (assuming they have one).

Do they not? If not, then bummer...

like image 947
Paul Knopf Avatar asked Feb 11 '12 14:02

Paul Knopf


People also ask

What is Jsonp API?

“JavaScript with Padding” (JSONP) Also called “JSON with Padding”, it is a technique for fooling a web browser into performing cross-origin requests using a special <script> tag that uses the src attribute to make a special API request.

What is Jsonp data type?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.


2 Answers

This is an old question nowadays you can get to the source through: https://raw.githubusercontent.com

https://raw.githubusercontent.com/ user / repository / branch / directory / file

e.g. https://raw.githubusercontent.com/sachatrauwaen/OpenContent-Templates/master/Bootstrap3Accordion/schema.json

like image 134
Peter Avatar answered Oct 26 '22 14:10

Peter


There is an API to get the contents of data from github. It's part of the v3 github API.

You make a request to

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

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

Unless you set the accepts header, you'll receive back some JSON with the file contents encoded in base64. You'll have to decode this, something that is very easy in node.js, but more of a pain in the browser. You can find base64 decoders in javascript in other questions on stackoverflow fairly easily. One thing to notice, the base64 code you get back from github has newline characters in it to make it format nicely and many base64 decoders can't cope with newlines, so you might need to remove them or modify the decoder.

You probably just want the content and don't need the other stuff in the json (such as 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're using node or curl, that's probably fine, but if you're running inside the browser, to do that you'll need to use CORS. Github only allow access from hosts that are registered as OAuth Applications. It's not particularly difficult to do this, but for my usecase (a bookmarketlet), that wasn't an option.

There is a way to get access without using CORS, and that is with JSONP, you can add e.g. ?callback=_processGithubResponse to get javascript output suitable for including with a script tag (that calls a function called _processGithubResponse with the response). Unfortunately you can't set an accepts header on that, so you're stuck with decoding base64 in this case.

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

like image 28
kybernetikos Avatar answered Oct 26 '22 13:10

kybernetikos