AnsweredAssumed Answered

HCP Anywhere API - Get Access Token Using Python

Question asked by John Devoy Employee on Jan 3, 2019
Latest reply on Jan 7, 2019 by Thorsten Simons

I am trying to use Python to generate an Access Token for HCP Anywhere.  I have created the following curl command and can successfully generate a token

 

curl https://hcp-aw-e.devmac.local/fss/public/login/oauth -ku "user:passwork" \

    -H "Content-Type: application/x-www-form-urlencoded" \

    -H "Accept: application/json" \

    -H "X-HCPAW-FSS-API-VERSION: 4.0.0" \

    -d "grant_type=urn:hds:oauth:negotiate-client"

 

 

I created the following Python 3 script to accomplish the same thing using the pycurl library and I am getting a 400 error.

 

import json

import pycurl

import urllib

 

USER = "user"

PASSWD = "password"

data = urllib.parse.urlencode({

    'grant_type': ['urn', 'hds', 'oauth', 'negotiate-client']

})

 

c = pycurl.Curl()

c.setopt(c.URL, https://hcp-aw-e.devmac.local/fss/public/login/oauth )

c.setopt(c.USERPWD, USER + ':' + PASSWD)

c.setopt(c.HTTPHEADER, ["Accept: application/json"])

c.setopt(c.HTTPHEADER, ["X-HCPAW-FSS-API-VERSION: 4.0.0"])

c.setopt(c.HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"])

c.setopt(pycurl.POSTFIELDS, data)

c.setopt(pycurl.SSL_VERIFYPEER, 0)

c.setopt(pycurl.SSL_VERIFYHOST, 0)

c.perform()

 

print (c.getinfo(c.RESPONSE_CODE))

c.close()

 

I think the issue is with the way am sending the oauth grant type data.  I have tried several different syntax options but can not get it to work.  

 

Any advice is greatly appreciated.

Outcomes