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:05] – [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 |
- | * [[https:// | + | * Header authentication using the HTTP headers ' |
- | * Header Authentication using the HTTP headers ' | + | * [[https:// |
- | * Cookie | + | * Cookie |
- | Note: Both the email address (e.g. "user@example.com" | + | Note: Both the email address (e.g. "testuser@example.com" |
- | ==== How to authenticate ==== | + | |
- | A POST http request to the API endpoint “/ | + | ===== / |
- | HTTP header fields: | + | [POST] In order to obtain a session cookie you can use the authentication endpoint. |
- | | < | + | |
- | | ::: |Either < | + | Note: For this endpoint, in addition |
- | | < | + | < |
- | | ::: |Either < | + | |
- | |< | + | |
- | === Examples === | + | |
- | == Request HTTP == | + | |
- | Example | + | |
- | <code> | + | |
- | https:// | + | |
- | </ | + | |
- | Request header: | + | |
- | <code json> | + | |
{ | { | ||
- | | + | |
- | Host: your_environment.protogrid.com | + | |
- | | + | |
- | user_secret: example_secret | + | |
} | } | ||
</ | </ | ||
- | == Request jQuery == | ||
- | Example in jQuery: | ||
- | < | + | ===== Examples obtaining a session cookie using the authentication endpoint with header authentication ===== |
+ | |||
+ | ==== HTTP ==== | ||
+ | <code> | ||
+ | POST / | ||
+ | Host: example.protogrid.com | ||
+ | username: testuser@example.com | ||
+ | password: test_password | ||
+ | </ | ||
+ | |||
+ | ==== jQuery ==== | ||
+ | <code javascript> | ||
$.ajax({ | $.ajax({ | ||
type:' | type:' | ||
Line 41: | Line 38: | ||
dataType: ' | dataType: ' | ||
beforeSend: function(xhr){ | beforeSend: function(xhr){ | ||
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
} | } | ||
}); | }); | ||
</ | </ | ||
- | == Request Python | + | ==== Python |
- | Example in Python | + | |
<code python> | <code python> | ||
+ | import requests | ||
url = " | url = " | ||
- | headers = dict(user_id="test_user@testdomain.com", | + | headers = dict(username="testuser@example.com", |
req = requests.post(url, | req = requests.post(url, | ||
response = req.text | response = req.text | ||
Line 58: | Line 55: | ||
</ | </ | ||
- | == Request Axios == | + | ==== Axios ==== |
- | Example with Axios | + | |
<code javascript> | <code javascript> | ||
const axios = require(' | const axios = require(' | ||
Line 65: | Line 61: | ||
headers: { | headers: { | ||
' | ' | ||
- | 'user_email': 'test_user@testdomain.com', | + | 'username': 'testuser@example.com', |
- | 'user_secret': ' | + | 'password': ' |
} | } | ||
}) | }) | ||
.then((result) => { | .then((result) => { | ||
- | console.log(' | + | console.log(' |
var cookies_from_resp = res.headers[' | var cookies_from_resp = res.headers[' | ||
var cookie_for_session = cookies_from_resp[0].split(';' | var cookie_for_session = cookies_from_resp[0].split(';' | ||
Line 77: | Line 73: | ||
}) | }) | ||
.catch((error) => { | .catch((error) => { | ||
- | console.error(' | + | console.error(' |
}); | }); | ||
</ | </ | ||
- | == Success Response == | + | ==== Success Response |
Example response of successful authentication: | Example response of successful authentication: | ||
- | < | + | < |
{ | { | ||
" | " | ||
- | " | + | " |
" | " | ||
} | } | ||
</ | </ | ||
- | == Unsuccessful | + | ==== Error Response |
Example response of unsuccessful authentication: | Example response of unsuccessful authentication: | ||
<code javascript> | <code javascript> | ||
{ | { | ||
- | | + | |
{ | { | ||
- | | + | |
- | | + | |
- | address and password.” | + | |
} | } | ||
], | ], | ||
- | " | + | " |
- | | + | |
} | } | ||
</ | </ | ||
- | ==== 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 121: | 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 137: | 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 149: | 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); | ||
}); | }); | ||
</ | </ | ||