所以,我做了如下變化 -
新增的依賴回來的build.gradle -
schoolCompile('com.google.api-client:google-api-client-android:1.22.0') {
exclude group: 'org.apache.httpcomponents'
}
然後,創建憑證對象旁邊的googleApiClient
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(activity.getApplicationContext().getResources().getString(R.string.firebase_client_id))
.requestEmail()
.requestProfile()
.requestScopes(new Scope(ClassroomScopes.CLASSROOM_COURSES_READONLY), new Scope(ClassroomScopes.CLASSROOM_ROSTERS_READONLY))
.requestServerAuthCode(auth_client_id)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.enableAutoManage(activity /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addConnectionCallbacks(this)
//.addOnConnectionFailedListener(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mCredential = GoogleAccountCredential.usingOAuth2(
activity.getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff());
做了登錄使用mGoogleApiClient -
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
activity.startActivityForResult(signInIntent, REQUEST_ACCOUNT_PICKER);
當該完成(在onActivityResult中),設置憑證上的電子郵件 -
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
mCredential.setSelectedAccountName(acct.getEmail());
} else {
// Signed out, show unauthenticated UI.
Log.i("GoogleAuthHelper", "Log in failed:"+result.getStatus());
}
連接到教室的時候創建服務作爲之前使用的憑據 -
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.classroom.Classroom.Builder(
transport, jsonFactory, credential)
.setApplicationName("Kindergarten Math School")
.build();
而且,這工作。在登錄過程中,我被要求授權額外的課堂範圍。課堂呼叫成功完成。仍然清理上面的代碼,但是,它的工作原理!
設置所選帳戶的位置,還可以使用: mCredential.setSelectedAccount(acct.getAccount()); 它確實做了同樣的事情,但可能會更具可讀性。 – Zoccadoum
我認爲使用setSelectedAccount而不是setSelectedAccountName存在一些權限問題。現在可能會被修復。值得一試。 –