markdown使用githubapi的curl教程(代码片段)

author author     2022-12-17     812

关键词:

# Introduction #

An introduction to [`curl`](http://curl.haxx.se/) using [GitHub's API](https://developer.github.com/guides/getting-started/#overview).

# The Basics #

Makes a basic GET request to the specifed URI

    curl https://api.github.com/users/caspyin

Includes HTTP-Header information in the output

    curl --include https://api.github.com/users/caspyin

Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile

    curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
    curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin

Passing just the username without the colon (`:`) will cause you to be prompted for your account password. This avoids having your password in your command line history

    curl --user "caspyin" https://api.github.com/users/caspyin

## POST ##

Use the `--request` (`-X`) flag along with `--data` (`-d`) to POST data

    curl --user "caspyin" --request POST --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists
    
    curl --user "caspyin" -X POST --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists

Of course `--data` implies POST so you don't have to also specify the `--request` flag

    curl --user "caspyin" --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists

Here is an example that uses the old GitHub API (v2). You can use multiple `--data` flags

    curl --data "login=caspyin" --data "token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

The post data gets combined into one so you can also just combine them yourself into a single `--data` flag

    curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

You can tell curl to read from a file (`@`) to POST data

    curl --user "caspyin" --data @data.txt https://api.github.com/gists 

Or it can read from STDIN (`@-`)

    curl --user "caspyin" --data @- https://api.github.com/gists
    
      "description":"Test",
      "public":false,
      "files": 
        "file1.txt": 
          "content":"Demo"
        
      
    
    end with ctrl+d

### Headers ###

Often when POSTing data you'll need to add headers for things like auth tokens or setting the content type. You can set a header using `-H`.

    curl -H "Content-Type: application/json" -H "authToken: 349ab29a-xtab-423b-a5hc-5623bc39b8c8" --data '' https://api.example.com/endpoint


### Dealing with HTTPS ###

If an API doens't have an SSL cert but is using HTTPS you can tell curl to ignore the security by using `--insecure`. Be warned this is a very **"insecure"** thing to do and is only listed here for "educational purposes".

    curl --insecure https://api.example.com/endpoint

For my own reference mostly, here is where I first learned about using `--insecure` https://github.com/wayneeseguin/rvm/issues/1684

# OAuth #

The first thing to know is that your API Token (found in https://github.com/settings/admin) is not the same token used by OAuth. They are different tokens and you will need to generate an OAuth token to be authorized.

Follow the API's instructions at http://developer.github.com/v3/oauth/ under the sections "Non-Web Application Flow" and "Create a new authorization" to become authorized.

Note: Use Basic Auth once to create an OAuth2 token http://developer.github.com/v3/oauth/#oauth-authorizations-api

    curl https://api.github.com/authorizations \
    --user "caspyin" \
    --data '"scopes":["gist"],"note":"Demo"'

This will prompt you for your GitHub password and return your OAuth token in the response. It will also create a new Authorized application in your account settings https://github.com/settings/applications

Now that you have the OAuth token there are two ways to use the token to make requests that require authentication (replace "OAUTH-TOKEN" with your actual token)

    curl https://api.github.com/gists/starred?access_token=OAUTH-TOKEN
    curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/gists/starred

List the authorizations you already have

    curl --user "caspyin" https://api.github.com/authorizations


# Resources #

* HTTParty - Ruby library that makes it easy to create HTTP requests https://github.com/jnunemaker/httparty
* Hurl IT - An open source web application to play with curl options http://hurl.it
# Introduction #

An introduction to [`curl`](http://curl.haxx.se/) using [GitHub's API](https://developer.github.com/guides/getting-started/#overview).

# The Basics #

Makes a basic GET request to the specifed URI

    curl https://api.github.com/users/caspyin

Includes HTTP-Header information in the output

    curl --include https://api.github.com/users/caspyin

Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile

    curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
    curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin

Passing just the username without the colon (`:`) will cause you to be prompted for your account password. This avoids having your password in your command line history

    curl --user "caspyin" https://api.github.com/users/caspyin

## POST ##

Use the `--request` (`-X`) flag along with `--data` (`-d`) to POST data

    curl --user "caspyin" --request POST --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists
    
    curl --user "caspyin" -X POST --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists

Of course `--data` implies POST so you don't have to also specify the `--request` flag

    curl --user "caspyin" --data '"description":"Created via API","public":"true","files":"file1.txt":"content":"Demo"' https://api.github.com/gists

Here is an example that uses the old GitHub API (v2). You can use multiple `--data` flags

    curl --data "login=caspyin" --data "token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

The post data gets combined into one so you can also just combine them yourself into a single `--data` flag

    curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

You can tell curl to read from a file (`@`) to POST data

    curl --user "caspyin" --data @data.txt https://api.github.com/gists 

Or it can read from STDIN (`@-`)

    curl --user "caspyin" --data @- https://api.github.com/gists
    
      "description":"Test",
      "public":false,
      "files": 
        "file1.txt": 
          "content":"Demo"
        
      
    
    end with ctrl+d

### Headers ###

Often when POSTing data you'll need to add headers for things like auth tokens or setting the content type. You can set a header using `-H`.

    curl -H "Content-Type: application/json" -H "authToken: 349ab29a-xtab-423b-a5hc-5623bc39b8c8" --data '' https://api.example.com/endpoint


### Dealing with HTTPS ###

If an API doens't have an SSL cert but is using HTTPS you can tell curl to ignore the security by using `--insecure`. Be warned this is a very **"insecure"** thing to do and is only listed here for "educational purposes".

    curl --insecure https://api.example.com/endpoint

For my own reference mostly, here is where I first learned about using `--insecure` https://github.com/wayneeseguin/rvm/issues/1684

# OAuth #

The first thing to know is that your API Token (found in https://github.com/settings/admin) is not the same token used by OAuth. They are different tokens and you will need to generate an OAuth token to be authorized.

Follow the API's instructions at http://developer.github.com/v3/oauth/ under the sections "Non-Web Application Flow" and "Create a new authorization" to become authorized.

Note: Use Basic Auth once to create an OAuth2 token http://developer.github.com/v3/oauth/#oauth-authorizations-api

    curl https://api.github.com/authorizations \
    --user "caspyin" \
    --data '"scopes":["gist"],"note":"Demo"'

This will prompt you for your GitHub password and return your OAuth token in the response. It will also create a new Authorized application in your account settings https://github.com/settings/applications

Now that you have the OAuth token there are two ways to use the token to make requests that require authentication (replace "OAUTH-TOKEN" with your actual token)

    curl https://api.github.com/gists/starred?access_token=OAUTH-TOKEN
    curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/gists/starred

List the authorizations you already have

    curl --user "caspyin" https://api.github.com/authorizations


# Resources #

* HTTParty - Ruby library that makes it easy to create HTTP requests https://github.com/jnunemaker/httparty
* Hurl IT - An open source web application to play with curl options http://hurl.it

如何通过 github API 使用 ssh 身份验证?

】如何通过githubAPI使用ssh身份验证?【英文标题】:HowtousesshauthenticationwithgithubAPI?【发布时间】:2013-02-0906:32:11【问题描述】:在通过命令行访问GitHubAPI时,是否有某种方法可以使用基于ssh的身份验证(例如,通过curl等?)。FW... 查看详情

markdown尝试在osge4wshell中使用curl时出现证书错误的解决方法(代码片段)

查看详情

shell中curl请求变更使用markdown

如果使用变量,得换成双引号,数据里面也有双引号,加转义字符即可。 curl'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d1b0f47c-9b50-452b-8467-e681e181af28'\\  -H'Content-Type:application/jso 查看详情

shell中curl请求变更使用markdown

...-Type:application/json'\\ -d"    \\"msgtype\\":\\"markdown\\",  \\"markdown\\":    \\"content\\":\\"[发票主流程构建失败报告]($TEST_REPORT)\\"  " 完整示例:#!/bin/bashfile="src/main/resources/config.propert... 查看详情

CURL 变量和解析 JSON 的问题 [重复]

...时间】:2022-01-1905:36:14【问题描述】:我的脚本正在使用githubAPI创建拉取请求:./my-script.shmy-repomy-branch#!/bin/bashRepo=$1Branch=$2cd$Repoget_data()cat<<EOF"t 查看详情

r使用r中的githubapi获取单个要点的完整内容(代码片段)

查看详情

作为研发如何使用githubapi?(代码片段)

...章目录使用步骤账号创建进行开发者相关设置API操作演示GithubAPI好处推荐的GithubAPI🌟个人主页:个人主页🚵‍♀️个人介绍:每天进步一点点,生活变得好一点点。       📌作为一位开发,不管是非工作的... 查看详情

ruby重命名文件使用githubapi(代码片段)

查看详情

markdown[curl]comofazercurl(代码片段)

查看详情

小白都能看得懂的教程一本教你如何在前端实现markdown编辑器(代码片段)

小白都能看得懂的教程一本教你如何在前端实现markdown编辑器  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、CSDN、GitHub、B站、华为开发者论坛等平台分享一些技术博文,主要包括前端开发、pyth... 查看详情

htmlajax-使用api​​(以githubapi为例)(代码片段)

查看详情

研发课堂丨手把手教你添加i.mx6ul对curl软件的支持

前言curl是一个开源项目,名字的含义是客户端(client)的URL工具的意思。主要的产品是curl(命令行工具)和libcurl(C语言的API库),两者功能均是:基于网络协议,对指定URL进行网络传输。... 查看详情

研发课堂丨手把手教你添加i.mx6ul对curl软件的支持

 前言curl是一个开源项目,名字的含义是客户端(client)的URL工具的意思。主要的产品是curl(命令行工具)和libcurl(C语言的API库),两者功能均是:基于网络协议,对指定URL进行网络传输... 查看详情

markdown向telegram发送通知消息(通过curl)(代码片段)

查看详情

手把手教你建github技术博客byhexo

...爱折腾的人熟练使用版本控制Git了解使用Github熟悉基本的MarkDown语法环境准备安装Git下载msysgit并执行即可完成安装。安装Node.js在Windows环境下安装Node.js非常简单,仅须下载安装文件并执行即可完成安装。安装hexo利用npm命令即可... 查看详情

带有 GitHub API 的 JavaScript XHR

】带有GitHubAPI的JavaScriptXHR【英文标题】:JavaScriptXHRw/GitHubAPI【发布时间】:2011-12-0510:01:44【问题描述】:我正在尝试使用XHR访问GitHubHTTPAPI,但我被同源策略严重击败。我很确定easyXDM对此太过分了,但我不知道我还能使用什么(... 查看详情

活动类 yodgobekkomilov.edgar.com.githubapi/yodgobekkomilov.edgar.com.githubapi.SearchActivity 不存在

】活动类yodgobekkomilov.edgar.com.githubapi/yodgobekkomilov.edgar.com.githubapi.SearchActivity不存在【英文标题】:Activityclassyodgobekkomilov.edgar.com.githubapi/yodgobekkomilov.edgar.com.githubapi.SearchActivitydoesnotexist活动类yodgob 查看详情

Github API - 向项目添加问题?

】GithubAPI-向项目添加问题?【英文标题】:GithubAPI-addanissuetoaproject?【发布时间】:2022-01-1506:07:24【问题描述】:是否可以使用githubAPI将问题添加到项目板?本文档:https://docs.github.com/en/rest/reference/projects#create-a-project-card表明这... 查看详情