2

示例代码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。我们如何使用它创建服务?

4

2 回答 2

0

您的应用程序向 Classroom API 发送的每个请求都必须包含一个授权令牌。该令牌还会向 Google 识别您的应用程序。您可以使用Google 登录OAuth 2.0

使用教室.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

有关 Classroom API 的更多详细信息,请查看此链接:https ://developers.google.com/classroom/reference/rest/

于 2016-07-13T06:55:16.863 回答
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();  

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

于 2016-07-13T22:58:20.100 回答