Unable to consume JAVA sdk for Google classroom API's getting PERMISSION_DENIED Request had insufficient authentication scopes -
i trying integrate google classroom in java project using oauth 2.0 server server authentication, example given here official google class room integration doc uses oauth consent screen authentication, altered code server server authentication few changes
progress far:
- created google app
enabled google classroom api dashboard.
created service account key (.p12) server-to-server authentication(later used in code along application name)
public class quickstart2 { private static final string application_name ="my_app_name"; private static final jsonfactory json_factory = jacksonfactory.getdefaultinstance(); private static httptransport httptransport; /** global instance of http transport. */ private static httptransport http_transport; private static final string service_account_email = "id_creaded_on_google_app"; /** global instance of scopes required quickstart. * * if modifying these scopes, delete saved credentials * @ ~/.credentials/classroom.googleapis.com-java-quickstart */ private static final list<string> scopes = arrays.aslist(classroomscopes.classroom_coursework_me); static { try { http_transport = googlenethttptransport.newtrustedtransport(); } catch (throwable t) { t.printstacktrace(); system.exit(1); } } /** * creates authorized credential object. * @return authorized credential object. * @throws ioexception * @throws generalsecurityexception */ public static credential authorize() throws ioexception, generalsecurityexception { /* added code in given sample code server-to-server authentication*/ httptransport = googlenethttptransport.newtrustedtransport(); googlecredential credential = new googlecredential.builder().settransport(httptransport) .setjsonfactory(json_factory) .setserviceaccountid(service_account_email) .setserviceaccountscopes(scopes) .setserviceaccountprivatekeyfromp12file(new file("bulb-1cb9e145138b.p12")) .setserviceaccountuser("bulb-88@bulb-173512.iam.gserviceaccount.com") .build(); return credential; } /** * build , return authorized classroom client service. * @return authorized classroom client service * @throws ioexception * @throws generalsecurityexception */ public static com.google.api.services.classroom.classroom getclassroomservice() throws ioexception, generalsecurityexception { credential credential = authorize(); system.out.println("token "+credential.getaccesstoken()); return new com.google.api.services.classroom.classroom.builder( http_transport, json_factory, credential) .setapplicationname(application_name) .build(); } public static void main(string[] args) throws ioexception, generalsecurityexception { // build new authorized api client service. com.google.api.services.classroom.classroom service = getclassroomservice(); course course = new course() .setname("10th grade biology") .setsection("period 2") .setdescriptionheading("welcome 10th grade biology") .setdescription("test descriptioon") .setroom("301") .setownerid("me") .setcoursestate("provisioned"); course = service.courses().create(course).execute(); /* here getting exception */ // list first 10 courses user has access to. listcoursesresponse response = service.courses().list() .setpagesize(10) .execute(); list<course> courses = response.getcourses(); system.out.println(courses); if (courses == null || courses.size() == 0) { system.out.println("no courses found."); } else { system.out.println("courses:"); (course course1 : courses) { system.out.printf("%s\n", course1.getname()); } } }
}
i belive nothing wrong authentication because whenever run piece of code, entry in dashboard on app "https://console.developers.google.com/apis/dashboard"
i new google class room , been long time trying integrating no success far, need help.
one more thing says mandatory have g suite account (i dont used, yet have not created g suit account, reason getting exception)
please let me know if suppose add more details on above senerio.
thanks in advance
gsuite accounts found here. classroom api scopes found in authorizing requests oauth 2.0
wiki
Comments
Post a Comment