2016-07-10 66 views
2

示例代碼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。我們如何使用它創建服務?

回答

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課堂API,檢查此鏈接:https://developers.google.com/classroom/reference/rest/

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

而且,這工作。在登錄過程中,我被要求授權額外的課堂範圍。課堂呼叫成功完成。仍然清理上面的代碼,但是,它的工作原理!

+0

設置所選帳戶的位置,還可以使用: mCredential.setSelectedAccount(acct.getAccount()); 它確實做了同樣的事情,但可能會更具可讀性。 – Zoccadoum

+0

我認爲使用setSelectedAccount而不是setSelectedAccountName存在一些權限問題。現在可能會被修復。值得一試。 –