alexa/alexa-skills-kit-sdk-for-python

Provide a minimal example of getting refresh token using access token

khushhalm opened this issue · 3 comments

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Other... Please describe:

Expected Behavior

I want to know how to get refresh token so that I can use the request library and send a get request and in response get the metrics data that I require.

Current Behavior

I'm using the request library to send a get request api.amazonalexa.com

r = requests.get(f"https://api.amazonalexa.com/v1/skills/{Skill_ID}/metrics?startTime={startTime}&endTime={endTime}&period={period}&metric={metric}&stage={stage}&skillType={skillType}", headers={'Authorization': access_token})

This works for fresh token but when access token expires this gives error and I'm not sure how to get new access token using refresh token.
I tried sending a post request but it doesn't work. Below is the post request:

headers = {'Content-type': Content_Type}
r = requests.post(f"https://api.amazonalexa.com/auth/o2/token?grant_type=refresh_token&refresh_token={refresh_token}&client_id={Client_ID}&client_secret={Client_Secret_Key}", headers=headers)

Possible Solution

Please either provide proper documentation of refresh token or a sample on how to get it.

Context

Can you please provide a minimal sample of how to use the ask smapi SDK to get access token from the refresh token?

Your Environment

  • ASK SDK for Python used: 1.23.0
  • Operating System and version: Mac OS 10.15.5

Python version info

  • Python version used for development: 3.7

Tried to use what's written on this page to get access token but seems like this only works for LWA.

Tried using this function from ask sdk but could not get any success.

Is there a way to use smapi_client to get the metrics data?

Hi @khushhalm, thanks for the raising the issue.
Unfortunately we do not have utils in ask-smapi-sdk to retrieve access token from the refresh token.
Although you can use ASK CLI utils to get the latest access token for usages in your requests code.

However I do recommend to use our existing API in ask-smapi-models through SMAPI SDK to achieve the same result.

from ask_smapi_sdk import StandardSmapiClientBuilder
smapi_client_builder = StandardSmapiClientBuilder(client_id='Client ID', client_secret='Client Secret Key', refresh_token='Refresh Token')
smapi_client = smapi_client_builder.client()
result = smapi_client. get_skill_metrics_v1(start_time, end_time, period, metric, stage, skill_type)

Let me know if you are able to obtain metrics date using the above API.

Thanks, @Shreyas-vgr for this

from ask_smapi_sdk import StandardSmapiClientBuilder
smapi_client_builder = StandardSmapiClientBuilder(client_id='Client ID', client_secret='Client Secret Key', refresh_token='Refresh Token')
smapi_client = smapi_client_builder.client()
result = smapi_client. get_skill_metrics_v1(start_time, end_time, period, metric, stage, skill_type)

Let me know if you are able to obtain metrics date using the above API.

I changed the last line to

result = smapi_client.get_skill_metrics_v1(skill_id= Skill_ID, start_time=start_time, end_time=end_time, period=period, metric=metric, stage=stage, skill_type=skill_type)

Since skill_id was missing but yeah this worked.
Although it would be great to have a function to get access token from refresh token when the current access token expires.

Thanks again 👍