GitHub jsonp源代码api [英] GitHub jsonp source code api

查看:158
本文介绍了GitHub jsonp源代码api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GitHub有一个jsonp api文件的来源吗?我知道BitBucket有,但我找不到GitHubs的任何信息(假设他们有一个)。

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...

推荐答案

有一个API可以从github获取数据的内容。它是 v3 github API 的一部分。

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

您提出请求到

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

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

除非您设置了接受标头,否则您将收到一些JSON文件,内容以base64编码。您必须对此进行解码,这在node.js中非常简单,但在浏览器中更加痛苦。你可以在其他问题上很容易地在javascript中找到base64解码器。有一点要注意,以base64编码你从GitHub回到中有换行符,使其很好地格式化和许多的base64解码器无法应对新行,所以你可能需要将其删除或修改解码器。

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.

你可能只是想要内容而不需要json中的其他东西(比如sha和length等),所以你可以通过设置Accept头来让你的生活更轻松到 application / vnd.github.3.raw

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.

下面是一个使用curl的accept头的例子:

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"

现在,如果您使用节点或卷曲,这可能很好,但如果你在浏览器内部运行,那么你需要使用CORS。 Github只允许注册为OAuth应用程序的主机访问。这样做并不是特别困难,但对于我的用例(bookmarketlet)来说,这不是一个选项。

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.

有一种方法可以在不使用CORS的情况下访问,这是与JSONP,你可以添加例如?回调= _processGithubResponse 来得到适合于包括与脚本标签(即呼叫被叫_processGithubResponse与响应函数)的JavaScript输出。不幸的是,你不能设置接受头,所以你在这种情况下坚持解码base64。

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.

如果你使用node.js,我会推荐您可以使用 node-github ,这使得API的使用更容易。

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

这篇关于GitHub jsonp源代码api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆