Solved

Getting "Unauthorized' when calling API with a valid token

  • 15 March 2024
  • 1 reply
  • 24 views

Hello friends,

We are new to Gainsight Digital Hub. I’m working on a script to migrate posts from a Zendesk forum to a category in Gainsight. I am able to retrieve a token with read write scope, but when I call the POST /conversations endpoint, I’m getting an Unauthorized response. I’ve tried with and without moderatorId and with and without authorId, and with just one or the other. 

I then decided to just try to list categories and get the same result. 

For completeness, here’s the code (in it’s current configuration):

 

def gdh_auth():

headers = {
"Content-Type": "application/x-www-form-urlencoded"
}

body = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
"scope": "write read"
}

response = requests.post("https://api2-us-west-2.insided.com/oauth2/token", data=body, headers=headers)

auth_json = response.json()

return auth_json['access_token']

def list_categories():
url = "https://api2-eu-west-1.insided.com/v2/categories?authorId=505"

headers = {
"Authorization": f"Bearer {auth_code}",
"Content-Type": "application/json"
}

response = requests.request(
"GET",
url,
headers=headers
)

print(f"categories list {response.text}")

def create_post(post):
url = "https://api2-eu-west-1.insided.com/v2/conversations/start?authorId=505&moderatorId=505"

headers = {
"Authorization": f"Bearer {auth_code}"
}

payload = {
"title": post['title'],
"content": post['details'],
"categoryId": 24,
"tags": [
"developer",
"archive"
],
"sticky": False,
"closed": True
}

print(f"headers {headers}")
print(f"payload: {payload}")

response = requests.post(url, data=payload, headers=headers)

print(f"response.headers: {response.headers}")
print(f"response body: {response.text}")

No matter what, I receive the following:
 

response.headers: {'Date': 'Fri, 15 Mar 2024 13:27:34 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '26', 'Connection': 'keep-alive', 'Server': 'openresty', 'WWW-Authenticate': 'Key realm="kong"'}

response body: {"message":"Unauthorized"}


user 505 is an admin, if that matters. 


Any pointers would be greatly appreciated.

icon

Best answer by shurrey 15 March 2024, 21:10

View original

1 reply

Just to close the loop, looks like I copied some code pointing to the wrong API top-level domain. Forget you ever saw this. LOL

Reply