所以,我做了以下更改 -
在 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();
而且,这行得通。在登录期间,我被要求授权额外的教室范围。课堂通话顺利通过。仍在清理上面的代码,但是,它有效!