Stack Overflow: A New Way for Developers to Get Support for Alexa

Now you can find Alexa-specific tags and topics in the AWS Collective on Stack Overflow. Ask questions and get help from badged experts and the Alexa developer community!

question

newuser-a90357ec-ed26-4224-a2bf-b23c3d874c54 avatar image

How to access user email?

Hi everyone,

I'm trying to access an user's email using getUpsServiceClient() (ASK SDK Documentation). I enabled the allow email option in the alexa console>Build>Permissions and enabled email permissions with a permissions card (Docs) However, it still does not work.. has anyone gotten this to work? What steps am I missing?

It doesn't work with getting the phone number either. Here's my code:

  const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput;
  const consentToken = requestEnvelope.context.System.user.permissions &&
                       requestEnvelope.context.System.user.permissions.consentToken;

  if (!consentToken) {
    return responseBuilder
      .speak(messages.NOTIFY_MISSING_EMAIL_PERMISSIONS)
      .withAskForPermissionsConsentCard('alexa::profile:email:read')
      .getResponse(); 
  }
  try {
    const deviceServiceClient = serviceClientFactory.getUpsServiceClient();
    console.log('REQUESTING SERVICE CLIENT: '+JSON.stringify(deviceServiceClient));
    const email = await deviceServiceClient.getProfileEmail();
    
    console.log(`Email successfully retrieved, now responding to user. Email: ${JSON.stringify(email)}`);
  }
  catch (error) {
    if (error.name !== 'ServiceError') {
      return responseBuilder.speak(messages.ERROR).getResponse();
    }
    throw error;
  }
alexa skills kitalexahow-toask clialexa skills challenge
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tsuneki@Amazon avatar image
Tsuneki@Amazon answered

Hi there,

Thank you for posting.

Could you try this code? It worked on my skill.

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  async handle(handlerInput) {
    const PERMISSIONS = [
      'alexa::profile:email:read'
    ];
    const { responseBuilder, serviceClientFactory } = handlerInput;
    try {
      const upsServiceClient = serviceClientFactory.getUpsServiceClient();  
      const email = await upsServiceClient.getProfileEmail();        

      const speechText = 'email address' + email;
      return handlerInput.responseBuilder
      .speak(speechText)
      .getResponse();
    } catch (error) {
      if (error.name == 'ServiceError') {
        console.log('ERROR StatusCode:' + error.statusCode + ' ' + error.message);
      }
      return responseBuilder
        .speak(messages.NOTIFY_MISSING_EMAIL_PERMISSIONS)
        .withAskForPermissionsConsentCard(PERMISSIONS)
        .getResponse();
    }
  },
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
  .addRequestHandlers(
    LaunchRequestHandler
  )
  .withApiClient(new Alexa.DefaultApiClient())
  .lambda();
3 comments
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Julien Buratto avatar image Julien Buratto commented ·

Doesn't work on my skill, I get:

	Syntax error in module 'index': SyntaxError
  async handle(handlerInput) {
        ^^^^^^
SyntaxError: Unexpected identifier
0 Likes 0 ·
Rokas avatar image Rokas Julien Buratto commented ·

What version of alexa SDK and what version of lambda are you using?

0 Likes 0 ·
elGambero avatar image elGambero commented ·

Hi!

I'm trying to use this code, but I get:

error: TypeError: serviceClientFactory.getUpsServiceClient is not a function

I'm using:

const Alexa = require('ask-sdk-core');

same with

const Alexa = require('ask-sdk');

in a Node.js 8.10 runtime in Lambda Management Console

tnx

0 Likes 0 ·
newuser-55548731-24fc-4117-be37-d54d405b99b9 avatar image
newuser-55548731-24fc-4117-be37-d54d405b99b9 answered


Hi, I had the same problem and I solved it by downloading the Alexa app, log in with my Amazon user.

Before calling the api, request the permissions (

{"type": "AskForPermissionsConsent",

"permissions": [

"alexa::profile:email:read"

]).

And in the App's home show it to accept them. That way I could get the apiAccessToken, with the permissions.

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-577481fe-fc0c-4ec9-9edc-cde1aa3f1cc1 avatar image
newuser-577481fe-fc0c-4ec9-9edc-cde1aa3f1cc1 answered

I also have issue in reading profile email id. My custom lambda is as below.

const InputTunerIntent = {

canHandle(handlerInput) {

const request = handlerInput.requestEnvelope.request;

return request.type === 'LaunchRequest'

||(request.type === 'IntentRequest' && request.intent.name === 'InputTunerIntent');

},

async handle(handlerInput) {

console.log("logging before checking token");

const { requestEnvelope, responseBuilder, serviceClientFactory } = handlerInput;

const consentToken = requestEnvelope.context.System.user.permissions && requestEnvelope.context.System.user.permissions.consentToken;

if (!consentToken) {

return responseBuilder

.speak('Please Provide Permissions!')

.withAskForPermissionsConsentCard(['alexa::profile:email:read'])

.getResponse();

}

else{

try{

console.log("before upsServiceClient");

const upsServiceClient = serviceClientFactory.getUpsServiceClient() ;

console.log("upsServiceClient", upsServiceClient);

const email = await upsServiceClient.getProfileEmail();

console.log(email);

const speechText = 'email address' + email;

return handlerInput.responseBuilder

.speak(speechText)

.getResponse();

}

catch(error) {

if (error.name == 'ServiceError') {

console.log('ERROR StatusCode:' + error.statusCode + ' ' + error.message);

}

}

}

console.log("logging after checking token");

},

}


const skillBuilder = Alexa.SkillBuilders.custom();



exports.handler = skillBuilder

.addRequestHandlers(


InputTunerIntent,

HelpHandler,

ExitHandler,

SessionEndedRequestHandler

)

.addErrorHandlers(ErrorHandler)

.withApiClient(new Alexa.DefaultApiClient())

.lambda();



Statements are not executed after

const upsServiceClient = serviceClientFactory.getUpsServiceClient() ; I could see logs before that and there are no errors in try catch block and execution directly jumps to

console.log("logging after checking token"); I am unable to read profile email id. Permissions are provided in Alexa skill and account is linked. There are no helpful logs cloud watch.

Can anyone please help me?


10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.