I am trying to make an Alexa skill using python backend.
I am using amazon developer console to create model and code backend.
I want to retrieve user email address.
I would appreciate if you could provide me with sample code. I tried many methods but none were working.
here are some codes I tried :
https://github.com/alexa/alexa-skills-kit-sdk-for-python/tree/master/samples/GetDeviceAddress
I know this is device address but this was also not working, and I thought if i could get address I can get email.
Everything mentioned online is for Node, and I want to make my backend on python
As specified in the official documentation you need to make an API call to the ASK.
For email you need to call your api_endpoint followed by /v2/accounts/~current/settings/Profile.email
For me endpoint is : https://api.eu.amazonalexa.com therefore the complete url becomes :
https://api.eu.amazonalexa.com/v2/accounts/~current/settings/Profile.email
As far as adding the token to authorize the request it can be done by using the requests library by passing header to the get request. You can learn more about that from here
The final code should then look something like this :
#Fetching access token
accesstoken = str(handler_input.request_envelope.context.system.api_access_token)
#Fetching user emailaddress from ASK API
endpoint = "https://api.eu.amazonalexa.com/v2/accounts/~current/settings/Profile.email"
api_access_token = "Bearer " + accesstoken
headers = {"Authorization": api_access_token}
r = requests.get(endpoint, headers=headers)
email = r.json()
Hope this helps, Cheers!
Related
I was trying to see if I could extract the values of the credit section (Written by, Produced by, etc) off a spotify track.
Using the Spotipy library,I was told there was a function for the same, (track_info['songwriters'] and track_audio_features(trackid)) but they dont work anymore.
Just looking for a solution for the same, irrespective of the library :)
Eg:
In the song "Stormzy - Own It(Feat. Ed Sheeran & Burna Boy)", the credits section is as follows -
Details under 'show Credits' section of the song
I'm trying to retrieve this info through python! :)
This information doesn't seem to be exposed in the spotify API: https://developer.spotify.com
So it seems like there is no way to get this information via a python-package that relies on the official API.
import requests
import json
# Set up the authentication parameters
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
# Request an access token
response = requests.post("https://accounts.spotify.com/api/token",
data={
"grant_type": "client_credentials"
},
auth=(client_id, client_secret))
# Extract the access token from the response
access_token = json.loads(response.text)["access_token"]
# Set up the headers for the API request
headers = {
"Authorization": f"Bearer {access_token}"
}
# Make the API request
track_id = "3n3Ppam7vgaVa1iaRUc9Lp"
response = requests.get(f"https://api.spotify.com/v1/tracks/{track_id}", headers=headers)
# Print the response
print(response.text)
This code snippet is just an example, you need to replace "YOUR_CLIENT_ID" and "YOUR_CLIENT_SECRET" with the actual values of your Spotify Web API client ID and secret, respectively. Also, this example retrieve the information of a specific track by providing its ID.
Also, You can do more operations like search tracks, artists, albums, etc. in the Spotify Web API by using similar code and different endpoints.
Please make sure you have read and understand the Spotify Web API's terms of use and developer policy before using it in any project.
I'm trying to fetch metadata from thoughtspot. I am able to call the url using browser and fetch the data. But here I'm trying to achieve it via python program. According to thougthspot documentation. I have to enable trusted authentication and pass my secret key & username to obtain a token which I can use in my program.
https://developers.thoughtspot.com/docs/?pageid=api-auth-session
my username : username#username.com
secret key : secret-key
Below is my code:(generated by postman)
import requests
url = "https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/session/auth/token?auth_token=secret-key&access_level=FULL&username=username#username.com"
payload={}
headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I'm getting Bad request error. Anyone here using thoughtspot over this issue. Appreciate your support very much.
Error I'm getting:
{"type":"Bad Request","description":"The server could not understand the request due to invalid syntax."}
I can fetch data by calling the api using a web-browser. Below url returns list of all meta-data objects. I want to achieve this using a python program (I have to authenticate first & call the below URL - Authentication step is not working for me when I tried to follow the documentation)
https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/metadata/list
Did you try changing the url so that it includes the domain name?
Also post the error you are getting. And a screenshot of a working request would be great!
Is it possible to change my Minecraft username with python using the mojang API? I’ve looked everywhere and can’t find anywhere that tells me how to do it, besides this documentation which I can’t understand. Does anyone know how?
The following will work:
import requests
name = "YOUR_NEW_USERNAME"
url = f"https://api.minecraftservices.com/minecraft/profile/name/{name}"
token = "YOUR_TOKEN"
headers = {'Authorization': f'Bearer {token}'}
response = requests.put(url,headers=headers)
I do not play Minecraft anymore, but the documentation states that you need to send a PUT request to https://api.minecraftservices.com/minecraft/profile/name/<name>, where <name> is your new username.
You can then check the response via response.json().
I am trying to capture my access token that is returned by an API during authentication process. The official method of doing the authentication process from the API is:
from xyz_api import accessToken
app_id = "your_app_id"
app_secret = "your_app_secret"
app_session = accessToken.SessionModel(app_id, app_secret)
response = app_session.auth()
The above will return a json like:
{
"code" : 200,
"data" : {
"authorization_code": "some random code"
},
}
Now, the following code is used to generate a URL:
authorization_code = “your_authorization_code”
app_session.set_token(authorization_code)
url = app_session.generate_token()
Now this is where I start having issue.
At this stage, what is recommended by the API author is:
1. Use the generated URL and copy paste it into a browser.
2. The browser will then do the authentication and return the access token to a redirect
url (I used http://localhost:5000).
3. Copy and paste the access token from redirect URL
What I want:
To be able to finish the authentication and get the access_token from
python code itself.
What I tried:
Using requests.get(url), but it doesn't work.
Is there any way to do this all using python only, without having a need to open a browser?
PS: The API I am trying to use is: https://api-docs.fyers.in/v1#authorization
Further Investigation
On further investigation, I discovered the following:
The API uses oauth
The URL obtained from the following code provided by the API author
url = app_session.generate_token()
is same as when I write the following code:
oauth = OAuth2Session('app_id')
authorization_url, state = oauth.authorization_url(url)
The url returned in both cases is of the form:
https://api.fyers.in/api/v1/genrateToken?authorization_code=some_auth_code&appId=my_app_id
If I copy paste this URL in a browser, it send back the access_token to my redirect_url (http://localhost:5000)
I am trying to get the access token using oauth::fetch_token with the following code, which is failing:
oauth1 = OAuth2Session('app_id', state=state, redirect_uri="http://localhost:5000")
token = oauth1.fetch_token('https://api.fyers.in/api/v1/genrateToken/',
client_secret='app_secret',
code='the_auth_code_returned')
Truly appreciate the help.
you are accessing only with Python...
you dont need to do anything manually in a browser..
This API not sure if what you wnat to.
I Believe you registred an APP , got your app_secret
the step
response = app_session.auth()
answer you with a authorization_code
"data" : {
"authorization_code": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqaGdqZzg3NiQ3ODVidjVANjQ3NTZ2NSZnNyM2OTg3Njc5OHhkIjoiWElHVFVYVjBSSSIsImV4cCI6MTU2MTU5NzM5Ny41NjQxNTV9.Agl-Uus63NforrUEdbG7YUlPcbFXu9hLYm4akGuIBkU"
you may access that authorization token like that
import json
r = json.dumps(reponse)
authorization_token = r['data']['authorization_code']
The authorization token AND you app_id must be passed as querystring to https://api.fyers.in/api/v1/genrateToken?
the REAL user will be then asked to accept login trought your app
and if accepted , the user will be redirected to the URL *I imagined you inform in your app register.
let me know if it make sense
I have been working on using the powerbi REST API and I haven't been able to properly make use of it. I made use of this and I was able to register an app and get as far as getting an access token, but still I get 401 statuses on my requests.
My major points of confusion are with regards to the app registration:
1) I am trying to read and write data from a python script. Is this a Native-App or a Web Side Server?
2) What is the meaning of the redirect and home urls on the app registration page? I am currently using my localhost:5000 with different /paths. Could this be the source of the issue?
3) My research indicates that there should be some sort of login interaction. I don't have one, is this an indication that something isn't being done properly?
My code is as follows:
import adal
import requests
AUTHORITY_URL = 'https://login.microsoftonline.com/{my_company}.onmicrosoft.com'
RESOURCE = 'https://analysis.windows.net/powerbi/api'
CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
def make_headers(access_token):
return {
'Authorization': "Bearer {}".format(access_token)
}
context = adal.AuthenticationContext(AUTHORITY_URL)
token = context.acquire_token_with_client_credentials(RESOURCE, CLIENT_ID, CLIENT_SECRET)
access_token = token['accessToken']
headers = make_headers(access_token)
url = "https://api.powerbi.com/v1.0/myorg/datasets"
resp = requests.get(url, headers=headers)
As I said above this works to give me an access token though a get a status 401 response on the request and there is no sign in prompt.
Any help/guidance would be tremendously appreciated.
1) In your case you should register a Native app.
2) Native apps has only Redirect URI. Redirect URI gives AAD more details about the specific application it authenticates. For Native apps you should set it to https://login.live.com/oauth20_desktop.srf.
3) It's hard to say why you are getting Unauthorized response. Check what rights you gave to your application - does it has rights to read or write all datasets? Try to decode the access token at https://jwt.io and look at scp - does it contain "Dataset.Read.All" or "Dataset.ReadWrite.All"?