简体   繁体   English

如何使用API​​创建GitHub Gist?

[英]How to create a GitHub Gist with API?

By looking at GitHub Gist API, I understood that it is possible to create the Gist create for anonymous users without any API keys/authentication. 通过查看GitHub Gist API,我了解到可以为匿名用户创建Gist创建而无需任何API密钥/身份验证。 Is it so? 是这样吗?

I could not find answers to following questions: 我找不到以下问题的答案:

  1. Are there any restrictions (number of gists) to be created etc? 是否有任何限制(要点数量)等?
  2. Is there any example that I can post the code from a form text input field to create a gist? 有没有例子我可以从表单文本输入字段发布代码来创建一个要点? I could not find any. 我找不到任何东西。

Thanks for any information about this. 感谢您提供相关信息。

Yes. 是。

From Github API V3 Documentation: 来自Github API V3文档:

For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. 对于使用基本身份验证或OAuth的请求,您每小时最多可以请求5,000个请求。 For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour. 对于未经身份验证的请求,速率限制允许您每小时最多发出60个请求。

For creating a gist, you can send a POST request as follows: 要创建一个要点,您可以发送POST请求,如下所示:

POST /gists

Here's an example I made: 这是我做的一个例子:

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

Refer to the documentation for more information. 有关更多信息,请参阅文档

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

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