如何使用GoogleApiClient提供课堂服务

时间:2016-07-10 21:15:46

标签: android google-classroom

示例代码https://developers.google.com/classroom/quickstart/android显示了在使用GoogleAcccountCredential类登录Google时如何调用服务。

    mService = new com.google.api.services.classroom.Classroom.Builder(
            transport, jsonFactory, credential)
            .setApplicationName("Kindergarten Math School")
            .build();

但是,随着我们现在使用play-services-auth的新版本,我们现在正在使用GoogleApiClient。我们如何用它创建服务?

2 个答案:

答案 0 :(得分:0)

您的应用程序发送到Classroom API的每个请求都必须包含授权令牌。该令牌还可识别您的Google应用程序。您可以使用Google Sign-inOAuth 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 Classroom API的详细信息,请查看以下链接:https://developers.google.com/classroom/reference/rest/

答案 1 :(得分:0)

所以,我做了以下更改 -

在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();  

而且,这很有效。在登录期间,我被要求授权额外的课堂范围。课堂电话顺利通过。仍在清理上面的代码,但是,它有效!