You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api-usage.en-us.md 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ---
  2. date: "2018-06-24:00:00+02:00"
  3. title: "API Usage"
  4. slug: "api-usage"
  5. weight: 40
  6. toc: true
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "advanced"
  11. name: "API Usage"
  12. weight: 40
  13. identifier: "api-usage"
  14. ---
  15. # OpenI API Usage
  16. ## Enabling/configuring API access
  17. By default, `ENABLE_SWAGGER` is true, and
  18. `MAX_RESPONSE_ITEMS` is set to 50. See [Config Cheat
  19. Sheet](https://docs.gitea.io/en-us/config-cheat-sheet/) for more
  20. information.
  21. ## Authentication via the API
  22. Gitea supports these methods of API authentication:
  23. - HTTP basic authentication
  24. - `token=...` parameter in URL query string
  25. - `access_token=...` parameter in URL query string
  26. - `Authorization: token ...` header in HTTP headers
  27. All of these methods accept the same API key token type. You can
  28. better understand this by looking at the code -- as of this writing,
  29. Gitea parses queries and headers to find the token in
  30. [modules/auth/auth.go](https://github.com/go-gitea/gitea/blob/6efdcaed86565c91a3dc77631372a9cc45a58e89/modules/auth/auth.go#L47).
  31. You can create an API key token via your Gitea installation's web interface:
  32. `Settings | Applications | Generate New Token`.
  33. ### OAuth2
  34. Access tokens obtained from Gitea's [OAuth2 provider](https://docs.gitea.io/en-us/oauth2-provider) are accepted by these methods:
  35. - `Authorization bearer ...` header in HTTP headers
  36. - `token=...` parameter in URL query string
  37. - `access_token=...` parameter in URL query string
  38. ### More on the `Authorization:` header
  39. For historical reasons, Gitea needs the word `token` included before
  40. the API key token in an authorization header, like this:
  41. ```
  42. Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675
  43. ```
  44. In a `curl` command, for instance, this would look like:
  45. ```
  46. curl -X POST "http://localhost:4000/api/v1/repos/test1/test1/issues" \
  47. -H "accept: application/json" \
  48. -H "Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675" \
  49. -H "Content-Type: application/json" -d "{ \"body\": \"testing\", \"title\": \"test 20\"}" -i
  50. ```
  51. As mentioned above, the token used is the same one you would use in
  52. the `token=` string in a GET request.
  53. ## API Guide:
  54. API Reference guide is auto-generated by swagger and available on:
  55. `https://gitea.your.host/api/swagger`
  56. or on
  57. [gitea demo instance](https://try.gitea.io/api/swagger)
  58. ## Listing your issued tokens via the API
  59. As mentioned in
  60. [#3842](https://github.com/go-gitea/gitea/issues/3842#issuecomment-397743346),
  61. `/users/:name/tokens` is special and requires you to authenticate
  62. using BasicAuth, as follows:
  63. ### Using basic authentication:
  64. ```
  65. $ curl --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
  66. [{"name":"test","sha1":"..."},{"name":"dev","sha1":"..."}]
  67. ```
  68. As of v1.8.0 of Gitea, if using basic authentication with the API and your user has two factor authentication enabled, you'll need to send an additional header that contains the one time password (6 digit rotating token). An example of the header is `X-Gitea-OTP: 123456` where `123456` is where you'd place the code from your authenticator. Here is how the request would look like in curl:
  69. ```
  70. $ curl -H "X-Gitea-OTP: 123456" --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
  71. ```
  72. ## Sudo
  73. The API allows admin users to sudo API requests as another user. Simply add either a `sudo=` parameter or `Sudo:` request header with the username of the user to sudo.