2021年3月10日星期三

Create a HMAC-SHA1 base 64 signature for API get request

The Problem: I am trying to get a requests.get to print from a website and it needs a signature. From what I read on the website it says that it needs to take my secret key and make a signature out of it using HMAC-SHA1 base 64.

I know I need to import hmac into my python script but other than that I am not sure if my signature will be what the website is looking for.

This is a link to how the authentication is suppose to be setup: https://www.ninjarmm.com/dev-api/#the_authentication_header

On the website it gives an example of how the signature should be but I don't know how to apply this in python:

### How do I apply this in my python script's header?  Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, Base64( UTF-8- Encoding-Of( StringToSign ) ) ) ); StringToSign = HTTP-Verb + "n" + Content-MD5 + "n" + Content-Type + "n" + Date + "n" + CanonicalizedResource;  ### How do I apply this in my python script's header?  

Here is my Code:

import requests  import os  import json  from datetime import date  from email.utils import formatdate    ninjaapi = "some data"  ninjasak = "some data"  ninjaurl = "https://api.ninjarmm.com/v1/devices"    ninjaheaders = {"authorization":ninjaapi + ninjasak, "Date":now,}  ninjastatusresponse = requests.get(ninjaurl, headers=ninjaheaders)   ninja_json = ninjastatusresponse.json()  print(ninja_json)    

Here is the results I get so far:

{'error': 'invalid_header', 'error_description': "Invalid 'Authorization' header", 'error_code': 1}    

If anyone has any ideas that I can test let me know!

Thank you for your time,

https://stackoverflow.com/questions/66542027/create-a-hmac-sha1-base-64-signature-for-api-get-request March 09, 2021 at 02:45PM

没有评论:

发表评论