1

私はawsコグニトに全く新しいです、そして、これらのガイドは全部の場所にあり、私は失われています。私のawsアカウントで私はアイデンティティプールを作ったが、今私はアンドロイドアプリから新しいユーザーを作成しようとしているが、ユーザーを作成できないか、コグニートプールに接続できない。私はあなたの指導のためにこの書き込みと希望をしているかどうかはわかりません!アンドロイドにCognito IDプールを設定するにはどうすればよいですか?

ここまでは私がこれまで持っていたものです。私は、モバイルハブを使用する場合

public class aws extends AppCompatActivity 
{ 
    private EditText firstName,lastName,email,password; 
    private Button loginButton; 
    private String poolId,clientId,clientSecret; 
    CognitoUserPool userPool; 
    CognitoUserAttributes userAttributes; 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 

     CognitoCachingCredentialsProvider credentialsProvider = new 
       CognitoCachingCredentialsProvider(
       getApplicationContext(), // Context 
       "IDENTITY POOL_ID", // Identity Pool ID 
       MY_REGION // Region 
     ); 
     CognitoSyncManager syncClient = new CognitoSyncManager(
       getApplicationContext(), // Context 
       MY_REGION, // Region 
       credentialsProvider 
     ); 
     Dataset dataset = syncClient.openOrCreateDataset("myDataset"); 
     dataset.put("myKey", "myValue"); 
     dataset.synchronize(new DefaultSyncCallback() { 
      @Override 
      public void onSuccess(Dataset dataset, List newRecords) { 
       //Your handler code here 
      } 
     }); 

     poolId = "MY_POOL_ID"; 
     clientId = "MY_CLIENT_ID"; 
     clientSecret = "MY_CLIENT_SECRET"; 
     ClientConfiguration clientConfiguration = new ClientConfiguration(); 
     // Create a CognitoUserPool object to refer to your user pool 
     userPool = new CognitoUserPool(getBaseContext(), poolId, clientId, clientSecret, clientConfiguration); 

     bindActivity(); 
    } 
    private void bindActivity() 
    { 
     firstName = (EditText) findViewById(R.id.register_firstNameET); 
     lastName = (EditText) findViewById(R.id.register_lastNameET); 
     email = (EditText) findViewById(R.id.register_emailET); 
     password = (EditText) findViewById(R.id.register_passwordET); 
     loginButton = (Button) findViewById(R.id.intro_register_zivit_button); 
     loginButton.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       setUpCognito(); 
      } 
     }); 
    } 
    private void setUpCognito() 
    { 
     // Create a CognitoUserAttributes object and add user attributes 
     userAttributes = new CognitoUserAttributes(); 
     // Add the user attributes. Attributes are added as key-value pairs 

     // Adding user's given name. 
     // Note that the key is "given_name" which is the OIDC claim for given name 
     userAttributes.addAttribute("given_name", firstName.getText().toString()); 

     // Adding user's lastName 
     userAttributes.addAttribute("family_Name", lastName.getText().toString()); 
     // Adding user's email address 
     userAttributes.addAttribute("email", email.getText().toString()); 

     setUpCognitoHandler(); 

    } 

    private void setUpCognitoHandler() 
    { 
     SignUpHandler signupCallback = new SignUpHandler() { 

      @Override 
      public void onSuccess(CognitoUser cognitoUser, boolean userConfirmed, CognitoUserCodeDeliveryDetails cognitoUserCodeDeliveryDetails) 
      { 
       Log.d("myCognito","sign up succeeded!"); 
       // Sign-up was successful 

       // Check if this user (cognitoUser) needs to be confirmed 
       if(!userConfirmed) 
       { 
        Log.d("myCognito","not confirmed! Need to confirm"); 
        confirmUser(); 
        // This user must be confirmed and a confirmation code was sent to the user 
        // cognitoUserCodeDeliveryDetails will indicate where the confirmation code was sent 
        // Get the confirmation code from user 
       } 
       else { 
        Log.d("myCognito","confirmed!"); 
        // The user has already been confirmed 
       } 
      } 

      @Override 
      public void onFailure(Exception exception) 
      { 
       Log.d("myCognito","sign up failed!"); 
       // Sign-up failed, check exception for the cause 
      } 
     }; 
     userPool.signUpInBackground("user1ID", password.getText().toString(), userAttributes, null, signupCallback); 
    } 

    private void confirmUser() 
    { 
     // Callback handler for confirmSignUp API 
     GenericHandler confirmationCallback = new GenericHandler() { 

      @Override 
      public void onSuccess() { 
       // User was successfully confirmed 
       Log.d("myCognito","Confirmed User Success!"); 
      } 

      @Override 
      public void onFailure(Exception exception) { 
       // User confirmation failed. Check exception for the cause. 
       Log.d("myCognito","Confirmed User faileure :("); 
      } 
     }; 
    } 
} 

このクラスを作成する方法については本当に混乱して再びイム、コードは、すべてのサンプルアプリで場所を超えていました。

答えて

1

それが作品に符号

以下

は(私はいくつかのことをコメントしているAuthencticationハンドラがどのようにすべての詳細を持っているCognitoUserPoolsSignInProvider

このファイルのモバイル・ハブルックでは、この文書Amazon Cognito Credentials Provider

をお試しくださいエラーを削除する)

private AuthenticationHandler authenticationHandler = new AuthenticationHandler() { 
    @Override 
    public void onSuccess(final CognitoUserSession userSession, final CognitoDevice newDevice) { 
     Log.i(LOG_TAG, "Logged in. " + userSession.getIdToken()); 

     cognitoUserSession = userSession; 

     if (null != resultsHandler) { 
      ViewHelper.showDialog(activity, activity.getString(title_activity_sign_in), 
        activity.getString(login_success) + " " + userSession.getIdToken()); 

      resultsHandler.onSuccess(CognitoUserPoolsSignInProvider.this); 
     } 

     initializedLatch.countDown(); 
    } 

    @Override 
    public void getAuthenticationDetails(
      final AuthenticationContinuation authenticationContinuation, final String userId) { 

     if (null != username && null != password) { 
      final AuthenticationDetails authenticationDetails = new AuthenticationDetails(
        username, 
        password, 
        null); 

      authenticationContinuation.setAuthenticationDetails(authenticationDetails); 
      authenticationContinuation.continueTask(); 
     } 

     initializedLatch.countDown(); 
    } 

    @Override 
    public void getMFACode(final MultiFactorAuthenticationContinuation continuation) { 
     multiFactorAuthenticationContinuation = continuation; 

     //todo uncomment it 
     /* final Intent intent = new Intent(context, MFAActivity.class); 
     activity.startActivityForResult(intent, MFA_REQUEST_CODE);*/ 
    } 

    @Override 
    public void authenticationChallenge(final ChallengeContinuation continuation) { 
     throw new UnsupportedOperationException("Not supported in this sample."); 
    } 

    @Override 
    public void onFailure(final Exception exception) { 
     Log.e(LOG_TAG, "Failed to login.", exception); 

     if (null != resultsHandler) { 
      ViewHelper.showDialog(activity, activity.getString(R.string.title_activity_sign_in), 
        "Failed" + " " + exception); 
      // activity.getString( //todo uncomment it) + " " + exception); 

        resultsHandler.onError(CognitoUserPoolsSignInProvider.this, exception); 
     } 

     initializedLatch.countDown(); 
    } 
}; 

これはonClickのリスナー

@Override 
    public View.OnClickListener initializeSignInButton(final Activity signInActivity, 
                 final View buttonView, 
                 final IdentityManager.SignInResultsHandler resultsHandler) { 

     this.activity = signInActivity; 
     this.resultsHandler = resultsHandler; 

     // User Pools requires sign in with the username or verified channel. 
     // Mobile Hub does not set up email verification because it requires SES verification. 
     // Hence, prompt customers to login using the username or phone number. 
     final EditText emailField = (EditText) activity.findViewById(EDIT_TEXT_USERNAME_ID); 
     emailField.setHint(R.string.button_text_sign_in); 


     final View.OnClickListener listener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       username = ViewHelper.getStringValue(activity, EDIT_TEXT_USERNAME_ID); 
       password = ViewHelper.getStringValue(activity, EDIT_TEXT_PASSWORD_ID); 

       final CognitoUser cognitoUser = cognitoUserPool.getUser(username); 

       cognitoUser.getSessionInBackground(authenticationHandler); 
      } 
     }; 

     buttonView.setOnClickListener(listener); 
     return listener; 
    } 
+0

もhttps://github.com/awslabs/aws-sdk-android-samples/tree/master/AmazonCognitoYourUserPoolsDemoこのgithubのサンプルコードを試しています – skarfa

関連する問題