如何使用GoogleApiClient的教室服务
问题描述:
示例代码https://developers.google.com/classroom/quickstart/android显示了在使用GoogleAcccountCredential类登录到Google时如何调用该服务。如何使用GoogleApiClient的教室服务
mService = new com.google.api.services.classroom.Classroom.Builder(
transport, jsonFactory, credential)
.setApplicationName("Kindergarten Math School")
.build();
但是,随着新版本我们现在使用play-services-auth,我们现在使用的是GoogleApiClient。我们如何使用它创建服务?
答
您的应用程序发送给Classroom API的每个请求都必须包含授权令牌。令牌还可以识别您的应用程序到Google。您可以使用Google Sign-in或OAuth 2.0
使用classroom.googleapis.com有不同的服务,如.courses。您可以使用该服务创建课程。
HTTP请求
POST https://classroom.googleapis.com/v1/courses
{
"id": string,
"name": string,
"section": string,
"descriptionHeading": string,
"description": string,
"room": string,
"ownerId": string,
"creationTime": string,
"updateTime": string,
"enrollmentCode": string,
"courseState": enum(CourseState),
"alternateLink": string,
"teacherGroupEmail": string,
"courseGroupEmail": string,
"teacherFolder": {
object(DriveFolder)
},
"courseMaterialSets": [
{
object(CourseMaterialSet)
}
],
}
需要他下面的OAuth范围:
https://www.googleapis.com/auth/classroom.courses
详情regaridng课堂API,检查此链接:https://developers.google.com/classroom/reference/rest/
答
所以,我做了如下变化 -
新增的依赖回来的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存在一些权限问题。现在可能会被修复。值得一试。 –