在 classroom:Request 中创建课程的身份验证范围不足

Creating Courses in classroom:Request had insufficient authentication scopes

在 google 教室 api 创建课程时遇到问题:

 { "code" : 403,
    "errors" : [ {
     "domain" : "global",
    "message" : "Request had insufficient authentication scopes.",
     "reason" : "forbidden"
     } ],
    "message" : "Request had insufficient authentication scopes.",
     "status" : "PERMISSION_DENIED"
     }

我的代码:

 private class MakeRequestTask extends AsyncTask {

        private com.google.api.services.classroom.Classroom mService = null;

        private Exception mLastError = null;

        MakeRequestTask(GoogleAccountCredential credential) {

            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            mService = new com.google.api.services.classroom.Classroom.Builder(
                    transport, jsonFactory, credential)
                    .setApplicationName("Classroom API Android Quickstart")
                    .build();
            Log.i("RR","mService"+mService);

        }

        /**
         * Background task to call Classroom API.
         *
         * @param params no parameters needed for this task.
         */
        @Override
        protected Object doInBackground(Object[] params) {
            try {
                Course course = new Course()
                        .setName("10th Grade Biology")
                        .setSection("Period 2")
                        .setDescriptionHeading("Welcome to 10th Grade Biology")
                        .setDescription("We'll be learning about about the structure of living creatures "
                                + "from a combination of textbooks, guest lectures, and lab work. Expect "
                                + "to be excited!")
                        .setRoom("301")
                        .setOwnerId("Bio10")
                        .setCourseState("PROVISIONED");
                course = mService.courses().create(course).execute();
                System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId());

           return course;
            } catch (Exception e) {
                mLastError = e;
                cancel(true);
                return null;
            }
        }




        @Override
        protected void onPreExecute() {
            mOutputText.setText("");
            mProgress.show();
        }

        @Override
        protected void onPostExecute(Object data) {
            mProgress.hide();
            if (data == null ) {
                mOutputText.setText("No results returned.");
            } else {
               // data.add(0, "Data retrieved using the Classroom API:");
                mOutputText.setText(String.valueOf(data));
            }
        }

        @Override
        protected void onCancelled() {
            mProgress.hide();
            if (mLastError != null) {
                if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
                    showGooglePlayServicesAvailabilityErrorDialog(
                            ((GooglePlayServicesAvailabilityIOException) mLastError)
                                    .getConnectionStatusCode());
                } else if (mLastError instanceof UserRecoverableAuthIOException) {
                    startActivityForResult(
                            ((UserRecoverableAuthIOException) mLastError).getIntent(),
                            Sample1.REQUEST_AUTHORIZATION);
                } else {
                    mOutputText.setText("The following error occurred:\n"
                            + mLastError.getMessage());
                    Log.i("RR","--"+mLastError.getMessage());
                }
            } else {
                mOutputText.setText("Request cancelled.");
            }
        }

    }

我不知道应该给ownerid起什么名字,这个代码是否正确。

您没有显示可能导致问题的代码。看来您正在使用需要此范围的 courses.create

Authorization Scopes Requires the following OAuth scope:

https://www.googleapis.com/auth/classroom.courses

但您使用的是 :

https://www.googleapis.com/auth/classroom.courses.readonly

此外,如果您要更改范围,请删除之前保存的凭据。