LoginSignup
2
1

More than 3 years have passed since last update.

AWS Amplify+IAM未認証ユーザでAppSyncを使う(Androidアプリ)

Last updated at Posted at 2019-09-09

参考

Web版があったので、こちらを参考にさせてもらいました。
https://qiita.com/youn4101/items/9bca97083f07d1b9af12

Android Studioのプロジェクトを新規作成

Basic Activityを作成し、ここに従う。
https://aws-amplify.github.io/docs/android/start

amplifyの設定

amplify configure
amplify init
amplify add api

? Please select from one of the below mentioned services GraphQL
? Provide API name: amplifyguestaccess
? Choose an authorization type for the API API key
? Do you have an annotated GraphQL schema? No
? Do you want a guided schema creation? Yes
? What best describes your project: Single object with fields (e.g., “Todo” with ID, name, description)
? Do you want to edit the schema now? Yes
Selected default editor not found in your machine. Please manually edit the file created at /Users/.../schema.graphql
? Press enter to continue 

ひとまず先述のURL( https://aws-amplify.github.io/docs/android/start
)をもとに、API Key でToDoのサンプルが動作するようにする。

Cognito, IAM

ここでは、 Cognito サービスにて、 amplify_guest_access というID プールを作成。
認証されていない ID に対してアクセスを有効にする にチェックを入れる

作成されたロールを確認し、 認証されていないロールCognito_amplify_guest_accessUnauth_Role)をIAMにて編集し、AppSyncへのアクセスを許可する。

{
    "Effect": "Allow",
    "Action": [
         "appsync:GraphQL"
    ],
    "Resource": "*"
}

awsconfiguration.json

    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "ap-northeast-1:b2bddc1a-f3bb-41fb-a93d-975f92457207",
                "Region": "ap-northeast-1"
            }
        }
    },
    "AppSync": {
        "Default": {
             (中略)
            "AuthMode": "AWS_IAM"
        }
    }

MainActivity.kt

        val credentialsProvider = CognitoCachingCredentialsProvider(applicationContext, AWSConfiguration(applicationContext))

        mAWSAppSyncClient = AWSAppSyncClient.builder()
            .context(applicationContext)
            .awsConfiguration( AWSConfiguration(applicationContext))
            .credentialsProvider(credentialsProvider)
            .build()

        runMutation() // 任意の場所で

上記が実行されると、DynamoDBのTableにItemが追加される。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1