Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
protogrid:json_api_authentication [2022-02-21 23:31] – [JSON API Authentication] dru | protogrid:json_api_authentication [2022-02-22 00:21] – dru | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== JSON API Authentication ====== | ====== JSON API Authentication ====== | ||
+ | All HTTP requests to the Protogrid JSON API require a valid authentication. If the authentication fails an HTTP error 403 will be returned. | ||
- | All HTTP requests to the Protogrid JSON API require a valid authentication. If the authentication fails, an HTTP error 403 will be returned. Currently, the following variants are available for authentication: | + | The following variants are available for authentication |
- | * Header | + | * Header |
- | * [[https:// | + | * [[https:// |
- | * Cookie | + | * Cookie |
- | Note: Both the email address (e.g. "user@example.com" | + | Note: Both the email address (e.g. "testuser@example.com" |
- | ==== / | + | |
+ | ===== / | ||
[POST] In order to obtain a session cookie you can use the authentication endpoint. | [POST] In order to obtain a session cookie you can use the authentication endpoint. | ||
- | === Examples === | + | Note: For this endpoint, in addition to the three variants above, the credentials can also be passed as JSON in the request body: |
+ | <code javascript> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Examples | ||
- | == HTTP == | + | ==== HTTP ==== |
< | < | ||
POST / | POST / | ||
Host: example.protogrid.com | Host: example.protogrid.com | ||
- | username: | + | username: |
password: test_password | password: test_password | ||
</ | </ | ||
- | == jQuery == | + | ==== jQuery |
<code javascript> | <code javascript> | ||
$.ajax({ | $.ajax({ | ||
Line 35: | Line 44: | ||
</ | </ | ||
- | == Python == | + | ==== Python |
<code python> | <code python> | ||
import requests | import requests | ||
url = " | url = " | ||
- | headers = dict(username=" | + | headers = dict(username=" |
req = requests.post(url, | req = requests.post(url, | ||
response = req.text | response = req.text | ||
Line 46: | Line 55: | ||
</ | </ | ||
- | == Request | + | ==== Axios ==== |
<code javascript> | <code javascript> | ||
const axios = require(' | const axios = require(' | ||
Line 68: | Line 77: | ||
</ | </ | ||
- | == Success Response == | + | ==== Success Response |
Example response of successful authentication: | Example response of successful authentication: | ||
- | < | + | < |
{ | { | ||
" | " | ||
Line 78: | Line 87: | ||
</ | </ | ||
- | == Unsuccessful | + | ==== Error Response |
Example response of unsuccessful authentication: | Example response of unsuccessful authentication: | ||
- | < | + | < |
{ | { | ||
- | | + | |
{ | { | ||
- | | + | |
- | | + | |
} | } | ||
], | ], | ||
" | " | ||
- | | + | |
} | } | ||
</ | </ | ||
- | ==== How to send authenticated http requests | + | ===== Examples using a previously obtained session cookie ===== |
- | Each request to any API endpoint has to be authenticated using the cookie returned by the / | + | |
- | === Examples | + | ==== HTTP ==== |
- | == ajax == | + | < |
- | Example ajax request: | + | GET / |
- | Note that when jQuery runs in a browser, that the cookie is passed automatically with the request. | + | Host: example.protogrid.com |
- | < | + | Cookie: session=.eJyNsjcfzO7DzDBQxq3cxhPBl1JzwkL4AnjUOkhrJWjN0bOGXd9dpeWmO-337efwDyf4bLA.YhNvyQ.PZSBKOhy94xZ8yLq-e0HwIqo |
- | $jq.ajax({ | + | </ |
+ | |||
+ | ==== jQuery ==== | ||
+ | < | ||
+ | $.ajax({ | ||
type: ' | type: ' | ||
url: ' | url: ' | ||
Line 107: | Line 119: | ||
dataType: ' | dataType: ' | ||
success: function(data) { | success: function(data) { | ||
- | | + | |
}, | }, | ||
- | error: function(data) { console.log(data); | + | error: function(data) { |
+ | | ||
+ | | ||
}); | }); | ||
</ | </ | ||
- | Note: Some browsers | + | Most browsers |
+ | |||
+ | In particular, this means that you usually don't need to worry about authentication if you use JSON API requests in [[protogrid: | ||
- | == Example Python | + | ==== Python |
- | Example | + | |
<code python> | <code python> | ||
- | # The cookie variable was set above in the authenticate | + | # The cookie variable was set above in the authentication |
url = " | url = " | ||
req = requests.get(url, | req = requests.get(url, | ||
Line 123: | Line 138: | ||
response = json.loads(response) | response = json.loads(response) | ||
</ | </ | ||
- | Note: For more information about the requests, please refer to http:// | + | For more information about the requests |
- | == Example Axios == | + | ==== Axios ==== |
- | Example request with Axios: | + | |
<code javascript> | <code javascript> | ||
axios.get(' | axios.get(' | ||
Line 135: | Line 149: | ||
}) | }) | ||
.then((result) => { | .then((result) => { | ||
- | console.log(' | + | console.log(' |
console.log(result.data); | console.log(result.data); | ||
}) | }) | ||
.catch((error) => { | .catch((error) => { | ||
- | console.log(' | + | console.log(' |
+ | console.log(error); | ||
}); | }); | ||
</ | </ | ||