Calling REST Api using Python

Not sure what I’m doing wrong here, localhost always returns unauthorised and api.safenet always times out.
Any help/basic guides appreciated! I am running launcher 0.8.0, MacOS

import requests
from requests.auth import HTTPDigestAuth
import json

url = 'http://localhost:8100/auth'
#url = 'http://api.safenet/auth'

payload = {
  "app": {
    "name": "Testing",
    "id": "blah",
    "version": "0.0.1",
    "vendor": "blahha"
  },
  "permissions": [
    "SAFE_DRIVE_ACCESS"
  ]
}

response = requests.get(url,data=payload)

Hello @mark ,

and very welcome to the SAFE Developer Community!

  1. Make sure the launcher is up and running, signed up and logged in.
  2. when you make the request to localhost the Launcher should come to the foreground ask you to confirm the permissions requested, confirm those

If the launcher isn’t running, you aren’t logged in or if you don’t confirm the permission soon enough, the request will time out. The api.safenet is just a virtual address that only resolves from within beaker-browser/the launcher but not outside of it (so not in any python scripts).

Hope this helped!

Staying tuned
Ben

Hi Ben,

Thanks for coming back so quickly! Launcher is up and logged in, no pop up appears when requesting on localhost. It is hitting launcher though since I am getting:

App Name: Anonymous Application, Request: Validate App Authorisation, Status: FAILURE in the Logs.
Malformed request perhaps?

Thanks

Heyah,

so, very well then. Just wanted to make sure the outer setup it alright. Then started looking into the code you provided and found the following problems:

  1. it is using GET though POST is needed
  2. it was missing the Content-Type-header (set to application/json)
  3. and the content body wasn’t encoded as json (I suspect requests would default to url-form-encode)

With these changes (see the full gist and its diff here), it prompts here properly now:

response = requests.post(url,
	data=json.dumps(payload),
	headers={'content-type': 'application/json'})
4 Likes

Aha - yep that did it, thank you sir!
Full code snippet for anyone playing around with safe in python:

import requests
import json

url = 'http://localhost:8100/auth'

params = dict(
    app=dict(name='My Awesome App',id='testid',version='0.0.1',vendor='blah'),
    permissions=["SAFE_DRIVE_ACCESS"]
)

response = requests.post(url,
	data=json.dumps(params),
	headers={'content-type': 'application/json'})

print (response.status_code)
5 Likes

hey @mark there already exists a python wrapper (with which i’m playing around this second :smiley: )

github-link is https://github.com/hintofbasil/PySafeAPI

of course no appendable data support yet (because it is some days older) but authentication and put/get stuff is already working - so a pretty good start!

2 Likes

Looks great! Thanks @mark!

Mind if I add it to the APP ZERO repo?

(APP ZERO is exactly this, making the step 1 SAFE Launcher request in various languages)

2 Likes

Go for it! Useful repo to have

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.