5

मैं AWS API Gateway Endpoint पर कॉल करना चाहता हूं जो AWS_IAMgenerated JavaScript API SDK का उपयोग कर सुरक्षित है।कॉग्निटो आईडी (+ कॉन्फ़िगरेशन) के साथ AWS API गेटवे एंडपॉइंट को कैसे कॉल करें?

मेरे पास Cognito UserPool और Cognito Identity Pool है। दोनों ClientId के माध्यम से ठीक से सिंक हो गए।

मैं Sign in के लिए इस कोड का उपयोग करें और Cognito Identity

AWS.config.region = 'us-east-1'; // Region 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here 
}); 

AWSCognito.config.region = 'us-east-1'; 
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here 
}); 

var poolData = { 
    UserPoolId: 'us-east-1_XXXXXXXX', 
    ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXX' 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 


var authenticationData = { 
    Username: 'user', 
    Password: '12345678', 
}; 
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
var userData = { 
    Username: 'user', 
    Pool: userPool 
}; 
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
cognitoUser.authenticateUser(authenticationDetails, { 
    onSuccess: function (result) { 
    console.log('access token + ' + result.getAccessToken().getJwtToken()); 

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXX', 
    IdentityId: AWS.config.credentials.identityId, 
    Logins: { 
     'cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXX': result.idToken.jwtToken 
    } 
    }); 

    AWS.config.credentials.get(function (err) { 
    // now I'm using authenticated credentials 
    if(err) 
    { 
     console.log('error in autheticatig AWS'+err); 
    } 
    else 
    { 
     console.log(AWS.config.credentials.identityId); 

    } 
    }); 
    }, 

    onFailure: function (err) { 
    alert(err); 
    } 

}); 

यह सब सफल हो जाते हैं और मैं अब एक authorized Cognito Identity है।

अब मैं API Gateway Endpoint को Lambda Function निष्पादित करने के लिए कॉल करने का प्रयास करता हूं।

var apigClient = apigClientFactory.newClient({ 
    accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY', 
    secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY', 
    sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token 
    region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1 
    }); 

    var params = { 
    // This is where any modeled request parameters should be added. 
    // The key is the parameter name, as it is defined in the API in API Gateway. 
    }; 

    var body = { 
    // This is where you define the body of the request, 
    query: '{person {firstName lastName}}' 
    }; 

    var additionalParams = { 
    // If there are any unmodeled query parameters or headers that must be 
    // sent with the request, add them here. 
    headers: {}, 
    queryParams: {} 
    }; 

    apigClient.graphqlPost(params, body, additionalParams) 
    .then(function (result) { 
     // Add success callback code here. 
     console.log(result); 
    }).catch(function (result) { 
    // Add error callback code here. 
    console.log(result); 
    }); 

लेकिन दुर्भाग्य से यह विफल हो जाता है। OPTIONS अनुरोध 200 के साथ सफल होता है लेकिन POST तब 403 के साथ विफल रहता है।

मुझे पूरा यकीन है कि CORS कोई समस्या नहीं है।

मुझे यकीन है कि समस्या IAM Roles और AWS Resource Configurations के साथ करना है।

मेरा प्रश्न मूल रूप से है, क्या आप कृपया मुझे सभी आवश्यक AWS Resource Configurations और IAM Roles प्रदान कर सकते हैं जो इस काम के लिए आवश्यक हैं?

संसाधन मैं है

  • एपीआई गेटवे कर रहे हैं - तैनात एपीआई Endpoints
  • लैम्ब्डा समारोह के साथ - Endpoint से बुलाया
  • cognito उपयोगकर्ता पूल - App के साथ पहचान पूल
  • cognito पहचान के लिए synced पूल - अधिकृत और अनधिकृत भूमिका के साथ मैप किया गया।
  • आईएएम भूमिकाएं - लैम्ब्डा फ़ंक्शन के लिए और कॉग्निटो पहचान पूल की अधिकृत और अनधिकृत भूमिका।

लेकिन मुझे नहीं पता कि इन संसाधनों को काम करने के लिए ठीक से कॉन्फ़िगर करने की आवश्यकता है।

धन्यवाद

उत्तर

3

cognito पहचान की क्या भूमिका पहुँच अनुमतियाँ है? सुनिश्चित करें कि आपके एपीआई पर execute-api:Invoke करने के लिए इसका उपयोग है।

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": [ 
     "execute-api:Invoke"   
     ], 
     "Resource": [ 
     "arn:aws:execute-api:us-east-1:<account>:<rest-api>/*/POST/graphql" 
     ] 
    } 
    ] 
} 

आप वेब कंसोल में विधि सेटिंग्स पृष्ठ से सटीक संसाधन एआरएन प्राप्त कर सकते हैं।

+0

बहुत बढ़िया नहीं है, धन्यवाद। वह पहेली का गुम टुकड़ा था। – Christine

1

सबकुछ का पालन करने के बाद भी मुझे एक ही त्रुटि मिल रही थी। और apigClient शुरू करते समय मुझे "sessionToken" याद आया था।

var apigClient = apigClientFactory.newClient({ 
accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY', 
secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY', 
sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token 
region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1 }); 

// वैकल्पिक: आप अस्थायी क्रेडेंशियल्स का उपयोग कर रहे हैं आप सत्र टोकन शामिल करना चाहिए - वास्तव में वैकल्पिक

+0

कॉग्निटो के साथ, आप अस्थायी प्रमाण-पत्रों का उपयोग कर रहे हैं। लेकिन कॉग्निटो का उपयोग करना स्वयं वैकल्पिक है, आप केवल मानक प्रमाण-पत्रों का उपयोग कर सकते हैं, लेकिन इसकी अनुशंसा नहीं की जाती है। वैसे भी, आप * कर सकते थे *, यही कारण है कि उस टिप्पणी का कहना है कि यह वैकल्पिक है। –

संबंधित मुद्दे