Gitea API Usage

Enabling/configuring API access

By default, ENABLE_SWAGGER_ENDPOINT is true, andMAX_RESPONSE_ITEMS is set to 50. See Config CheatSheet for moreinformation.

Authentication via the API

Gitea supports these methods of API authentication:

  • HTTP basic authentication
  • token=… parameter in URL query string
  • access_token=… parameter in URL query string
  • Authorization: token … header in HTTP headers
    All of these methods accept the same apiKey token type. You canbetter understand this by looking at the code – as of this writing,Gitea parses queries and headers to find the token inmodules/auth/auth.go.

You can create an apiKey token via your gitea install’s web interface:Settings | Applications | Generate New Token.

More on the Authorization: header

For historical reasons, Gitea needs the word token included beforethe apiKey token in an authorization header, like this:

  1. Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675

In a curl command, for instance, this would look like:

  1. curl -X POST "http://localhost:4000/api/v1/repos/test1/test1/issues" \
  2. -H "accept: application/json" \
  3. -H "Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675" \
  4. -H "Content-Type: application/json" -d "{ \"body\": \"testing\", \"title\": \"test 20\"}" -i

As mentioned above, the token used is the same one you would use inthe token= string in a GET request.

Listing your issued tokens via the API

As mentioned in#3842,/users/:name/tokens is special and requires you to authenticateusing BasicAuth, as follows:

Using basic authentication:

  1. $ curl --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
  2. [{"name":"test","sha1":"..."},{"name":"dev","sha1":"..."}]

原文: https://docs.gitea.io/zh-cn/api-usage/