1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I need help with Python signature hash for Mozscape api

Discussion in 'Other Languages' started by Sebastian Rooks, Aug 4, 2016.

  1. Sebastian Rooks

    Sebastian Rooks Registered Member

    Joined:
    Nov 16, 2015
    Messages:
    82
    Likes Received:
    54
    Location:
    Middle America
    Hey guys, I've been struggling for a full day, trying to get the signature hash right for the Mozscape
    api.

    I find the support materials to be a little lacking. Googling hasn't gotten me anywhere. To me, it looks like it should be working to their specifications, but all I get is a 401.

    If anyone is using the Mozscape api with Python, I'd really appreciate a little help in forming a successful call. Maybe you don't use Mozscape specifically, but you're more familiar with hashing and encoding than I am? Either way, I could use a bit of a nudge on this one.

    Thanks guys

    Edit: Problem solved, here's working code if anyone finds themselves in the same position. Note the shoddy url encoding.

    Code:
    #this bitch right here is going to get me into the moz api
    import time, hmac, base64, hashlib, math, json, requests
    
    # ID and KEY from http://www.seomoz.org/api
    ACCESS_ID = ""
    SECRET_KEY = ""
    URL = "www.moz.com" #or whatever
    
    expires = int(time.time() + 300)
    toSign  = '%s\n%i' % (ACCESS_ID, expires)
    
    params = base64.b64encode(hmac.new(SECRET_KEY.encode('ascii'), toSign.encode('ascii'), hashlib.sha1).digest())
    params=str(params).replace('b','')
    params=str(params).replace('=','%3D')
    params=str(params).replace('/','%2F')
    params=str(params).replace("'",'')
    
    url1="https://lsapi.seomoz.com/linkscape/url-metrics/" + URL + "?"+"Cols=4&Limit=10&AccessID=" + str(ACCESS_ID) + "&Expires=" +str(expires)+"&Signature="+params
    print(url1)
    
    try:
        req = requests.get(url1)
        print(req.text)
       
    except Exception as e:
        print(e)
    
    
     
    • Thanks Thanks x 1
    Last edited: Aug 4, 2016