Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
protogrid:json_api_authentication [2017-12-08 09:57] – 46.140.51.3 | protogrid:json_api_authentication [2022-02-22 23:09] (current) – [/api/v2/authenticate] 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. | ||
- | At the moment only Basic Authentication is supported. If you need OAuth2 please contact us at [[mailto:protogrid-support@protogrid.com|protogrid-support@protogrid.com]]. All http requests require an authentication. If the authentication fails, an error will be returned. The error describes whether | + | The following variants are available for authentication in all JSON API endpoints: |
+ | * Header authentication using the HTTP headers ' | ||
+ | * [[https://en.wikipedia.org/ | ||
+ | * Cookie | ||
- | === How to authenticate === | + | Note: Both the email address (e.g. " |
- | 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 < | + | |
- | |< | + | |
- | Example | + | |
- | <code> | + | |
- | https:// | + | |
- | </ | + | |
- | Request Header: | + | |
- | <code json> | + | |
{ | { | ||
- | POST / | + | " |
- | Host: your_environment.protogrid.com | + | " |
- | user_id: example_user | + | |
- | user_secret: example_secret | + | |
} | } | ||
</ | </ | ||
- | 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({ | ||
- | | + | |
- | url: ' | + | url: ' |
- | contentType: | + | contentType: |
- | dataType: ' | + | dataType: ' |
- | beforeSend: function(xhr){ | + | beforeSend: function(xhr){ |
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
- | } | + | } |
}); | }); | ||
</ | </ | ||
- | Example in Python | + | ==== 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 50: | Line 55: | ||
</ | </ | ||
+ | ==== Axios ==== | ||
+ | <code javascript> | ||
+ | const axios = require(' | ||
+ | axios.post(' | ||
+ | headers: { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }) | ||
+ | .then((result) => { | ||
+ | console.log(' | ||
+ | var cookies_from_resp = res.headers[' | ||
+ | var cookie_for_session = cookies_from_resp[0].split(';' | ||
+ | | ||
+ | // send authenticated http request here (see documentation below) | ||
+ | }) | ||
+ | .catch((error) => { | ||
+ | console.error(' | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | ==== Success Response ==== | ||
Example response of successful authentication: | Example response of successful authentication: | ||
- | < | + | < |
{ | { | ||
- | " | + | |
- | " | + | " |
- | " | + | " |
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Error Response ==== | ||
Example response of unsuccessful authentication: | Example response of unsuccessful authentication: | ||
- | < | + | < |
{ | { | ||
- | “errors”: [ | + | "errors": [ |
- | { | + | { |
- | “code”: 401, | + | " |
- | “message”: “Your login wasn’t recognized. | + | " |
- | address and password.” | + | } |
- | } | + | ], |
- | ], | + | " |
- | " | + | " |
- | “result”: {} | + | |
} | } | ||
</ | </ | ||
- | How to send authenticated http requests | + | ===== Examples |
- | Each request to the API must be authenticated | + | |
- | Example ajax request: | + | ==== HTTP ==== |
- | Note that when jQuery | + | < |
- | $jq.ajax({ | + | GET /api/v2/apps |
- | type: ' | + | Host: example.protogrid.com |
- | url: ' | + | Cookie: session=.eJyNsjcfzO7DzDBQxq3cxhPBl1JzwkL4AnjUOkhrJWjN0bOGXd9dpeWmO-337efwDyf4bLA.YhNvyQ.PZSBKOhy94xZ8yLq-e0HwIqo |
- | contentType: | + | </ |
- | dataType: ' | + | |
- | success: function(data) { | + | ==== jQuery |
- | console.log(data); | + | <code javascript> |
- | }, | + | $.ajax({ |
- | error: function(data) { console.log(data); | + | type: ' |
+ | url: ' | ||
+ | contentType: | ||
+ | dataType: ' | ||
+ | success: function(data) { | ||
+ | console.log(data); | ||
+ | }, | ||
+ | error: function(data) { | ||
+ | | ||
+ | | ||
}); | }); | ||
- | Note: Some browsers | + | </ |
- | Example | + | Most browsers |
- | The cookie variable was set above in the authenticate | + | |
+ | In particular, this means that you usually don't need to worry about authentication if you use JSON API requests in [[protogrid: | ||
+ | |||
+ | ==== Python | ||
+ | <code python> | ||
+ | # The cookie variable was set above in the authentication | ||
url = " | url = " | ||
req = requests.get(url, | req = requests.get(url, | ||
response = req.text | response = req.text | ||
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 | ||
+ | |||
+ | ==== Axios ==== | ||
+ | <code javascript> | ||
+ | axios.get(' | ||
+ | headers: { | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }) | ||
+ | .then((result) => { | ||
+ | console.log(' | ||
+ | console.log(result.data); | ||
+ | }) | ||
+ | .catch((error) => { | ||
+ | console.log(' | ||
+ | console.log(error); | ||
+ | }); | ||
+ | </ | ||