質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

Q&A

0回答

607閲覧

Android StudioにてJAVAをつかったGoogleアカウント認証

ladyinredjes

総合スコア12

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

Authentication

Authentication(認証)は正当性を認証する為の工程です。ログイン処理等で使われます。

0グッド

0クリップ

投稿2021/03/23 17:08

編集2021/03/24 20:28

前提・実現したいこと

Android StudioでJAVAを使ったアプリ開発をしています。
ユーザー登録の際に
グーグル認証を使った新規ユーザのアカウント登録とログインをしたいのですが
どうしてもエラーが解決できません。

ユーザーがログインするLOGINとログイン後にユーザの情報が表示されるMYPAGE二つのページで構成されています。

発生している問題・エラーメッセージ

@Override
protected void onStart() {
super.onStart();

FirebaseUser user = mAuth.**getCurrentUser();** if(user!=null){ Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); startActivity(intent); }

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.**signInWithCredential**(credential) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information FirebaseUser user = mAuth.**getCurrentUser();** Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show(); }
上記太字の部分が解決できないメソッドとしてエラーになります。

該当のソースコード

Java

1package com.example.alertappfyp2.ui.login; 2 3 4public class LoginActivity extends AppCompatActivity { 5 6 7 private GoogleSignInClient mGoogleSignInClient; 8 private final static int RC_SIGN_IN = 123; 9 private FirebaseAuth mAuth; 10 11 12 @Override 13 protected void onStart() { 14 super.onStart(); 15 16 17 FirebaseUser user = mAuth.**getCurrentUser();** 18 if(user!=null){ 19 Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); 20 startActivity(intent); 21 } 22 23 24 } 25 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 31 setContentView(R.layout.activity_main); 32 33 34 mAuth = FirebaseAuth.getInstance(); 35 36 37 createRequest(); 38 39 40 findViewById(R.id.login).setOnClickListener(new View.OnClickListener() { 41 @Override 42 public void onClick(View view) { 43 signIn(); 44 } 45 }); 46 47 48 } 49 50 51 private void createRequest() { 52 53 54 // Configure Google Sign In 55 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 56 .requestIdToken(getString(R.string.default_web_client_id)) 57 .requestEmail() 58 .build(); 59 60 61 // Build a GoogleSignInClient with the options specified by gso. 62 mGoogleSignInClient = GoogleSignIn.getClient(this, gso); 63 64 65 } 66 67 68 private void signIn() { 69 Intent signInIntent = mGoogleSignInClient.getSignInIntent(); 70 startActivityForResult(signInIntent, RC_SIGN_IN); 71 } 72 73 74 @Override 75 public void onActivityResult(int requestCode, int resultCode, Intent data) { 76 super.onActivityResult(requestCode, resultCode, data); 77 78 79 // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 80 if (requestCode == RC_SIGN_IN) { 81 Task task = GoogleSignIn.getSignedInAccountFromIntent(data); 82 try { 83 // Google Sign In was successful, authenticate with Firebase 84 GoogleSignInAccount account = (GoogleSignInAccount) task.getResult(ApiException.class); 85 firebaseAuthWithGoogle(account); 86 } catch (Throwable e) { 87 // Google Sign In failed, update UI appropriately 88 // ... 89 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 90 } 91 } 92 } 93 94 95 private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 96 97 98 AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 99 mAuth.**signInWithCredential**(credential) 100 .addOnCompleteListener(this, new OnCompleteListener() { 101 @Override 102 public void onComplete(@NonNull Task task) { 103 if (task.isSuccessful()) { 104 // Sign in success, update UI with the signed-in user's information 105 FirebaseUser user = mAuth.getCurrentUser(); 106 Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); 107 startActivity(intent); 108 109 110 } else { 111 Toast.makeText(LoginActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show(); 112 113 114 } 115 116 117 // ... 118 } 119 }); 120 } 121 122 123}

Java

1 2public class MyPage extends AppCompatActivity { 3 4 ImageView img; 5 TextView name,Age,pol; 6 Button LB; 7 8 FirebaseAuth mAuth; 9 GoogleSignInClient mGoogleSignInClient; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_my_page); 15 16 img=findViewById(R.id.img); 17 name=findViewById(R.id.name); 18 Age=findViewById(R.id.Age); 19 pol=findViewById(R.id.pol); 20 LB=findViewById(R.id.lb); 21 22 GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); 23 if(signInAccount != null){ 24 name.setText(signInAccount.getDisplayName()); 25 Age.setText(signInAccount.getEmail()); 26 } 27 28 29 LB.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View view) { 32 FirebaseAuth.getInstance().signOut(); 33 Intent intent = new Intent(getApplicationContext(),MainActivity.class); 34 startActivity(intent); 35 } 36 }); 37 } 38 } 39 40 41

grade

1// Top-level build file where you can add configuration options common to all sub-projects/modules. 2 3buildscript { 4 repositories { 5 google() 6 jcenter() 7 8 } 9 dependencies { 10 classpath 'com.android.tools.build:gradle:4.1.2' 11 classpath 'com.google.gms:google-services:4.3.5' 12 13 // NOTE: Do not place your application dependencies here; they belong 14 // in the individual module build.gradle files 15 } 16} 17 18allprojects { 19 repositories { 20 google() 21 jcenter() 22 23 } 24} 25

grade

1apply plugin: 'com.android.application' 2 3android { 4 compileSdkVersion 29 5 buildToolsVersion "29.0.2" 6 defaultConfig { 7 applicationId "com.example.alertappfyp2" 8 minSdkVersion 29 9 targetSdkVersion 29 10 versionCode 1 11 versionName "1.0" 12 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 vectorDrawables.useSupportLibrary = true 14 } 15 buildTypes { 16 release { 17 minifyEnabled false 18 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 19 } 20 } 21} 22 23dependencies { 24 implementation fileTree(dir: 'libs', include: ['*.jar']) 25 implementation 'androidx.appcompat:appcompat:1.0.2' 26 implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 27 implementation 'com.google.android.material:material:1.0.0' 28 implementation 'androidx.annotation:annotation:1.0.2' 29 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' 30 implementation 'androidx.preference:preference:1.1.0-alpha05' 31 implementation 'androidx.vectordrawable:vectordrawable:1.0.1' 32 implementation 'androidx.navigation:navigation-fragment:2.0.0' 33 implementation 'androidx.navigation:navigation-ui:2.0.0' 34 implementation platform('com.google.firebase:firebase-bom:26.4.0') 35 implementation 'com.google.firebase:firebase-messaging:' 36 implementation 'com.google.firebase:firebase-analytics' 37 implementation 'com.google.firebase:firebase-core' 38 implementation 'com.google.firebase:firebase-admin:7.1.0' 39 implementation 'com.google.firebase:firebase-auth:19.2.0' 40 implementation "com.firebaseui:firebase-ui-auth:7.1.1" 41 implementation 'com.google.android.gms:play-services-auth:19.0.0' 42 implementation 'com.github.bumptech.glide:glide:4.4.0' 43 testImplementation 'junit:junit:4.12' 44 androidTestImplementation 'androidx.test:runner:1.1.1' 45 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 46} 47 48apply plugin: 'com.google.gms.google-services' 49 50

試したこと

多数の方法で試してみましたがどうしても解決できません。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/03/24 00:18

build.gradle (Project と Module それぞれ) はどうなってますか?
ladyinredjes

2021/03/24 20:29

追加しました。どうぞよろしくお願い致します。
hoshi-takanori

2021/03/24 20:47

firebase-admin はサーバー用なので、Android アプリでは使えないはず…。 あと、全体的にライブラリが古かったり、firebase-bom を使うなら firebase-auth のバージョンは指定する必要がなかったりしますね。
ladyinredjes

2021/03/25 09:49

上記編集したところ、エラーすべて解決できました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問