Android Question [SOLVED] AWS Cognito js Pre-Script

BillMeyer

Well-Known Member
Licensed User
Longtime User
Hi Fellow B4X'ers

A client has request for me to use AWS Cognito. I have no idea of what or how to do it. Can someone help please ???

They have given me the following pre-script I must run then a cURL...

Here is the pre-script (how do I run/convert it to B4A so that I can use it)


js Pre- Script:
var clientId = pm.environment.get("cognitoClientId");
var clientSecret = pm.environment.get("cognitoClientSecret");
var username = pm.environment.get("cognitoUserName");
var password = pm.environment.get("cognitoUserPassword");
pm.sendRequest({
           url: "https://cognito-idp.us-east-2.amazonaws.com/",
           method: 'POST',
           header: {
                    'X-Amz-Target':   'AWSCognitoIdentityProviderService.InitiateAuth',
                    'Content-Type': 'application/x-amz-json-1.1'
                   },
            body: {
                   mode: 'raw',
                   raw: JSON.stringify({
                   "AuthParameters": {
                   "USERNAME": username,
                   "PASSWORD": password
                   },
                  "AuthFlow": "USER_PASSWORD_AUTH",
                  "ClientId": clientId
  }),
options: {
raw: {
language: 'json'
}
}
}
}, function (error, response) {
console.log(response.json());
pm.environment.set("cognitoAccessToken", response.json().AuthenticationResult.AccessToken);
pm.environment.set("cognitoIdToken", response.json().AuthenticationResult.IdToken);
pm.environment.set("token", response.json().AuthenticationResult.AccessToken);
});

Thank you in anticipation...
(Anymore information required - please request - I'll post asap)
 
Solution
Test with valid data to check if it's ok

B4X:
    Dim Link As String = "https://cognito-idp.us-east-2.amazonaws.com/"
   
    Dim Body As Map = CreateMap("AuthParameters": CreateMap( "username" : "xxx", "password" : "xxx"), "AuthFlow": "USER_PASSWORD_AUTH", "ClientId": "xxx")
    Dim Parameters  As String = Body.As(JSON).ToString
    Log(Parameters)
   
    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
   
    Log("---------------")
'    Log(DataResult)
    Log(DataResult.As(JSON).ToMap.As(JSON).ToString)
B4X:
Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)...

TILogistic

Expert
Licensed User
Longtime User
Test with valid data to check if it's ok

B4X:
    Dim Link As String = "https://cognito-idp.us-east-2.amazonaws.com/"
   
    Dim Body As Map = CreateMap("AuthParameters": CreateMap( "username" : "xxx", "password" : "xxx"), "AuthFlow": "USER_PASSWORD_AUTH", "ClientId": "xxx")
    Dim Parameters  As String = Body.As(JSON).ToString
    Log(Parameters)
   
    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
   
    Log("---------------")
'    Log(DataResult)
    Log(DataResult.As(JSON).ToMap.As(JSON).ToString)
B4X:
Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetContentType("application/x-amz-json-1.1")
        j.GetRequest.SetHeader("x-amz-target","AWSCognitoIdentityProviderService.InitiateAuth")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        Else
            Result = j.Response.ErrorResponse
        End If
    Catch
        Log(LastException)
    End Try
    Log("Status: " & j.Response.StatusCode)
    j.Release
    Return Result
End Sub

1687571546393.png
 
Last edited:
Upvote 0
Solution

TILogistic

Expert
Licensed User
Longtime User
Note:
I researched the APIs of AWS services.
and found that member @roumei wrote a class that you can use and modify for the "Cognito" service.

Class : AWSDynamoDB
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
@TILogistic - Thank you so much for the speedy reply and solution - It worked perfectly and thank you also for the further advice on the Class that @roumei made available - I will look into that and modify where I can and repost for the benefit of other members.

This help is greatly appreciated...
 
Upvote 0
Top