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 [2017-12-08 17:29] – cso | protogrid:json_api_authentication [2022-02-21 23:30] – [How to authenticate] dru | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== JSON API Authentication ====== | ====== JSON API Authentication ====== | ||
- | At the moment only Basic Authentication is supported. If you need OAuth2 please contact us at [[mailto: | + | All HTTP requests |
+ | * [[https:// | ||
+ | * Header | ||
+ | * Cookie Authentication using the session cookie returned after a successfull authentication with one of the upper two variants. | ||
- | ==== How to authenticate ==== | + | Note: Both the email address (e.g. " |
+ | ==== /api/v2/authenticate ==== | ||
+ | |||
+ | [POST] In order to obtain a session cookie you can use the authentication endpoint. | ||
- | A POST http request to the API endpoint “/ | ||
- | HTTP header fields: | ||
- | | < | ||
- | | ::: |Either < | ||
- | | < | ||
- | | ::: |Either < | ||
- | |< | ||
=== Examples === | === Examples === | ||
- | == Request | + | |
- | Example request: | + | == HTTP == |
< | < | ||
- | https:// | + | POST / |
- | </ | + | Host: example.protogrid.com |
- | Request header: | + | username: example_user |
- | <code json> | + | password: test_password |
- | { | + | |
- | POST / | + | |
- | Host: | + | |
- | user_id: example_user | + | |
- | user_secret: example_secret | + | |
- | } | + | |
</ | </ | ||
- | == Request jQuery == | ||
- | Example in jQuery: | ||
- | < | + | == jQuery == |
+ | < | ||
$.ajax({ | $.ajax({ | ||
- | | + | |
- | url: ' | + | url: ' |
- | contentType: | + | contentType: |
- | dataType: ' | + | dataType: ' |
- | beforeSend: function(xhr){ | + | beforeSend: function(xhr){ |
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
- | xhr.setRequestHeader(' | + | xhr.setRequestHeader(' |
- | } | + | } |
}); | }); | ||
</ | </ | ||
- | == Request | + | == Python == |
- | Example in Python (with requests): | + | |
<code python> | <code python> | ||
+ | import requests | ||
url = " | url = " | ||
- | headers = dict(user_id="test_user@testdomain.com", user_secret=" | + | headers = dict(username="testuser@example.com", user_secret=" |
req = requests.post(url, | req = requests.post(url, | ||
response = req.text | response = req.text | ||
response = json.loads(response) | response = json.loads(response) | ||
cookie = req.cookies[' | cookie = req.cookies[' | ||
+ | </ | ||
+ | |||
+ | == Request 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(' | ||
+ | }); | ||
</ | </ | ||
Line 58: | Line 72: | ||
<code json> | <code json> | ||
{ | { | ||
- | " | + | |
- | " | + | " |
- | " | + | " |
} | } | ||
</ | </ | ||
Line 68: | Line 82: | ||
<code json> | <code json> | ||
{ | { | ||
- | “errors”: | + | |
- | { | + | { |
- | “code”: | + | “code”: |
- | “message”: | + | “message”: |
- | address and password.” | + | } |
- | } | + | ], |
- | ], | + | " |
- | " | + | “result”: |
- | “result”: | + | |
} | } | ||
</ | </ | ||
Line 89: | Line 102: | ||
<code jquery> | <code jquery> | ||
$jq.ajax({ | $jq.ajax({ | ||
- | | + | |
- | url: ' | + | url: ' |
- | contentType: | + | contentType: |
- | dataType: ' | + | dataType: ' |
- | success: function(data) { | + | success: function(data) { |
- | console.log(data); | + | console.log(data); |
- | }, | + | }, |
- | error: function(data) { console.log(data); | + | error: function(data) { console.log(data); |
}); | }); | ||
</ | </ | ||
Line 104: | Line 117: | ||
Example Python request: | Example Python request: | ||
<code python> | <code python> | ||
- | The cookie variable was set above in the authenticate example. | + | # The cookie variable was set above in the authenticate example. |
url = " | url = " | ||
req = requests.get(url, | req = requests.get(url, | ||
Line 111: | Line 124: | ||
</ | </ | ||
Note: For more information about the requests, please refer to http:// | Note: For more information about the requests, please refer to http:// | ||
+ | |||
+ | == Example Axios == | ||
+ | Example request with Axios: | ||
+ | <code javascript> | ||
+ | axios.get(' | ||
+ | headers: { | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }) | ||
+ | .then((result) => { | ||
+ | console.log(' | ||
+ | console.log(result.data); | ||
+ | }) | ||
+ | .catch((error) => { | ||
+ | console.log(' | ||
+ | }); | ||
+ | </ | ||