简体   繁体   English

使用Github API添加新Gist

[英]Add New Gist using the Github API

I'm making a small app in Adobe Air which I need to interact with the Github Gist API. 我正在Adobe Air制作一个小应用程序,我需要与Github Gist API进行交互。 However I'm kind of stuck. 但是我有点卡住了。

If you're not familiar with Adobe Air you can still help, the XMLHttpRequest javascript object can do cross domain requests, as there is no domain as such. 如果您不熟悉Adobe Air,您仍然可以提供帮助, XMLHttpRequest javascript对象可以执行跨域请求,因为没有这样的域。 So theres nothing Adobe Air specific here. 所以这里没有Adobe Air的具体内容。

Where I'm stuck is I think I need to authenticate myself then make the POST. 在我被困的地方,我想我需要对自己进行身份验证然后进行POST。 I just don't understand it 我只是不明白

The problem with your script is that though you're sending a POST method, you're adding the data in the URL as though it were a GET. 您的脚本的问题是,虽然您正在发送POST方法,但您正在将URL中的数据添加为GET。 You just need to change xmlhttp.send(NULL) to xmlhttp.send(data) , where data is the query data you were appending to the gists URL before (including the file and authentication information). 您只需要将xmlhttp.send(NULL)更改为xmlhttp.send(data) ,其中data是您之前附加到gists URL的查询数据(包括文件和身份验证信息)。

As a simple example, here's an excerpt from a bash script creating a new gist: 举个简单的例子,这里是一个创建新gist 的bash脚本的摘录:

#!/usr/bin/env bash
if [ -z "$(git config github.token)" ]
then echo "warning: no api key found, to add follow instructions on github account page."
else echo "attempting to create a new gist using your github authentication..."; fi

SHA=$((curl https://gist.github.com/gists --include \
       --data login=$(git config github.user) \
       --data token=$(git config github.token) \
       --data action_button=private \
       --data 'file_ext[gistfile1]=txt' \
       --data 'file_contents[gistfile1]=Hello World, this is an example gist!' \
| perl -e 'for(<>){if(/^Location: https?:\/\/gist.github.com\/([0-9a-f]+)/){print $1}}')2>/dev/null)

echo "New example gist created at https://gist.github.com/$SHA"

You shouldn't have to authenticate the user. 您不必对用户进行身份验证。

The XMLHttpRequest will just have to have the user name and the users API token included in the request. XMLHttpRequest只需要在请求中包含用户名和用户API令牌。

Looking at the example ruby script provided by github here , you just have to provide the following attributes: 查看github提供的示例ruby脚本,您只需提供以下属性:

  • File Extenstion 文件扩展名
  • file name 文件名
  • content 内容
  • If the gist is private or not 如果要点是私人的或不是
  • User login 用户登录
  • User API Token 用户API令牌

python and perl versions of the script pythonperl版本的脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM