使用Github API添加新的代码片段

11

我正在用Adobe Air制作一个小应用程序,需要与Github Gist API进行交互。然而,我有些卡住了。

如果您不熟悉Adobe Air,也可以帮助我,因为XMLHttpRequest JavaScript对象可以进行跨域请求,因为没有域名的限制。所以这里并没有什么与Adobe Air相关的内容。

我的问题是,我认为我需要先进行身份验证,然后再进行POST请求,但我不理解它的具体实现方式。

2个回答

4
你的脚本问题在于,虽然你使用了POST方法,但是你把数据添加到URL中就像使用GET方法一样。你只需要将xmlhttp.send(NULL)改为xmlhttp.send(data),其中data是你之前附加到gists URL的查询数据(包括文件和身份验证信息)。
作为一个简单的例子,这里是从a bash script中创建新Gist的摘录:
#!/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"

我不明白POST数据必须是什么格式。你能再解释一下吗? - Ben Shelock
在你原来的脚本中,你有这行代码:"var url = gists+'?...'"。我所替换的内容就是你的POST数据中的所有部分"..."。 - Jeremy

1

您不需要对用户进行身份验证。

XMLHttpRequest 只需在请求中包含用户名和用户 API 令牌即可。

查看 此处的 github 提供的示例 Ruby 脚本,您只需提供以下属性:

  • 文件扩展名
  • 文件名
  • 内容
  • gist 是否为私有
  • 用户登录
  • 用户 API 令牌

PythonPerl 版本的脚本


我认为您的文件类型/名称和内容必须像示例一样加上前缀。'file_ext[gistfile1]' 'file_name[gistfile1]' 'file_contents[gistfile1]' - Brian Gianforcaro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接