如何使用API创建GitHub Gist?

6
通过查看GitHub Gist API,我了解到可以为匿名用户创建Gist而无需使用API密钥/身份验证。是这样吗?
我找不到以下问题的答案:
1. 是否有任何限制(要创建的Gist数量等)? 2. 是否有任何示例可以将来自表单文本输入字段的代码发布到gist中?我找不到任何。
感谢您提供有关此事的任何信息。

1
我非常确定未经身份验证的gist创建属于未经身份验证的速率限制请求类别。 - user2062950
1个回答

8

是的。

来自Github API V3文档:

对于使用基本身份验证或OAuth的请求,您可以每小时最多进行5,000次请求。对于未经身份验证的请求,速率限制允许您每小时进行最多60个请求。

要创建要点,请发送以下POST请求:

POST /gists

这是我做的一个例子:
<?php
if (isset($_POST['button'])) 
{    
    $code = $_POST['code'];

    # Creating the array
    $data = array(
        'description' => 'description for your gist',
        'public' => 1,
        'files' => array(
            'foo.php' => array('content' => 'sdsd'),
        ),
    );                               
    $data_string = json_encode($data);

    # Sending the data using cURL
    $url = 'https://api.github.com/gists';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    # Parsing the response
    $decoded = json_decode($response, TRUE);
    $gistlink = $decoded['html_url'];

    echo $gistlink;    
}
?>

<form action="" method="post">
Code: 
<textarea name="code" cols="25" rows="10"/> </textarea>
<input type="submit" name="button"/>
</form>

请参阅 文档 以获取更多信息。


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