【问题标题】:Add New Gist using the Github API使用 Github API 添加新 Gist
【发布时间】:2010-12-31 20:43:28
【问题描述】:

我正在 Adob​​e Air 中制作一个需要与 Github Gist API 交互的小应用程序。但是我有点卡住了。

如果您不熟悉 Adob​​e Air,您仍然可以提供帮助,XMLHttpRequest javascript 对象可以进行跨域请求,因为没有这样的域。所以这里没有特定的 Adob​​e Air。

我被困的地方是我认为我需要对自己进行身份验证然后进行 POST。就是没看懂

【问题讨论】:

    标签: javascript api air github


    【解决方案1】:

    您的脚本的问题在于,虽然您发送的是 POST 方法,但您将数据添加到 URL 中,就好像它是 GET 一样。您只需将xmlhttp.send(NULL) 更改为xmlhttp.send(data),其中data 是您之前附加到gists URL 的查询数据(包括文件和身份验证信息)。

    作为一个简单的例子,下面是 a bash script 创建新要点的摘录:

    #!/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 数据必须采用什么格式。请您进一步解释一下。
    • 在您的原始脚本中,您有一行:“var url = gists+'?...'”。您的帖子数据将是我用“...”替换的所有内容。
    【解决方案2】:

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

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

    查看github here提供的示例ruby脚本,您只需提供以下属性:

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

    pythonperl 版本的脚本

    【讨论】:

    • 我认为您的文件类型/名称和内容必须像示例一样加上前缀。 'file_ext[gistfile1]' 'file_name[gistfile1]' 'file_contents[gistfile1]'
    猜你喜欢
    • 2013-09-11
    • 2019-05-08
    • 1970-01-01
    • 2018-02-15
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多