Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
protogrid:json_api_authentication [2017-12-08 17:29] csoprotogrid:json_api_authentication [2022-02-21 23:05] – [JSON API Authentication] 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: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 a HTTP Header field is missing or the login data is incorrect.+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: 
 +  * [[https://en.wikipedia.org/wiki/Basic_access_authentication|HTTP Basic authentication (BA)]]. 
 +  * Header Authentication using the HTTP headers 'username' and 'password'
 +  * Cookie Authentication using the session cookie returned after a successfull authentication with one of the upper two variants.
  
 +Note: Both the email address (e.g. "user@example.com") and the user ID (e.g. "1957f847-f298-4f14-a031-7ffbe31aeb47") can be used for " username".
 ==== How to authenticate ==== ==== How to authenticate ====
  
Line 21: Line 25:
 <code json> <code json>
 { {
- POST /api/v2/authenticate +  POST /api/v2/authenticate 
- Host: your_environment.protogrid.com +  Host: your_environment.protogrid.com 
- user_id: example_user +  user_id: example_user 
- user_secret: example_secret+  user_secret: example_secret
 } }
 </code> </code>
Line 32: Line 36:
 <code jquery> <code jquery>
 $.ajax({ $.ajax({
-    type:'POST', +  type:'POST', 
-    url: 'https://example.protogrid.com/api/v2/authenticate', +  url: 'https://example.protogrid.com/api/v2/authenticate', 
-    contentType: 'application/json; charset=utf-8', +  contentType: 'application/json; charset=utf-8', 
-    dataType: 'json', +  dataType: 'json', 
-    beforeSend: function(xhr){ +  beforeSend: function(xhr){ 
-        xhr.setRequestHeader('user_id','tester@test.com'); +      xhr.setRequestHeader('user_id','tester@test.com'); 
-        xhr.setRequestHeader('user_secret','test_password'); +      xhr.setRequestHeader('user_secret','test_password'); 
-    }+  }
 }); });
 </code> </code>
Line 52: Line 56:
 response = json.loads(response) response = json.loads(response)
 cookie = req.cookies['session'] cookie = req.cookies['session']
 +</code>
 +
 +== Request Axios ==
 +Example with Axios
 +<code javascript>
 +const axios = require('axios');
 +axios.post('https://example.protogrid.com/api/v2/authenticate', {}, {
 +  headers: {
 +    'Content-Type': 'application/json; charset=utf-8',
 +    'user_email': 'test_user@testdomain.com',
 +    'user_secret': 'test_password'
 +  }
 +})
 +.then((result) => {
 +  console.log('Outer Success.');
 +  var cookies_from_resp = res.headers['set-cookie'];
 +  var cookie_for_session = cookies_from_resp[0].split(';').[0];
 +  
 +  // send authenticated http request here (see documentation below)
 +})
 +.catch((error) => {
 +  console.error('Outer Error: ' + error);
 +});
 </code> </code>
  
Line 58: Line 85:
 <code json> <code json>
 { {
- "errors": [], +  "errors": [], 
- "protogrid_environment_version": "1.3.9", +  "protogrid_environment_version": "1.3.9", 
- "result": "Login successful!"+  "result": "Login successful!"
 } }
 </code> </code>
Line 66: Line 93:
 == Unsuccessful Response == == Unsuccessful Response ==
 Example response of unsuccessful authentication: Example response of unsuccessful authentication:
-<code json>+<code javascript>
 { {
- “errors”:+  “errors”:
- +    
- “code”: 401, +      “code”: 401, 
- “message”: “Your login wasn’t recognized. Please check your e-mail +      “message”: “Your login wasn’t recognized. Please check your e-mail 
- address and password.” +      address and password.” 
- +    
- ],  +  ],  
- "protogrid_environment_version": "1.3.9", +  "protogrid_environment_version": "1.3.9", 
- “result”: {}+  “result”: {}
 } }
 </code> </code>
Line 89: Line 116:
 <code jquery> <code jquery>
 $jq.ajax({ $jq.ajax({
-        type: 'GET', +  type: 'GET', 
-        url: 'https://example.protogrid.com/api/v2/apps', +  url: 'https://example.protogrid.com/api/v2/apps', 
-        contentType: 'application/json; charset=utf-8', +  contentType: 'application/json; charset=utf-8', 
-        dataType: 'json', +  dataType: 'json', 
-        success: function(data) { +  success: function(data) { 
-                console.log(data); +          console.log(data); 
-        }, +  }, 
-        error: function(data) { console.log(data); }+  error: function(data) { console.log(data); }
 }); });
 </code> </code>
Line 104: Line 131:
 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 = "https://example.protogrid.com/api/v2/apps" url = "https://example.protogrid.com/api/v2/apps"
 req = requests.get(url, cookies=cookie) req = requests.get(url, cookies=cookie)
Line 111: Line 138:
 </code> </code>
 Note: For more information about the requests, please refer to http://docs.python-requests.org/en/master/ Note: For more information about the requests, please refer to http://docs.python-requests.org/en/master/
 +
 +== Example Axios ==
 +Example request with Axios:
 +<code javascript>
 +axios.get('https://example.protogrid.com/api/v2/apps', {
 +  headers: {
 +    'Content-Type': 'application/json; charset=utf-8',
 +    'Cookie': cookie_for_session // The cookie_for_session variable was set above in the authenticate example.
 +  }
 +})
 +.then((result) => {
 +  console.log('Inner Success.');
 +  console.log(result.data);
 +})
 +.catch((error) => {
 +  console.log('Inner Error: ' + error);
 +});
 +</code>
  
Print/export