android - 在教室 :Request had insufficient authentication scopes 中创建类(class)

标签 android google-classroom

在 google classroom api 中创建类(class)时出现问题:

 { "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

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

关于android - 在教室 :Request had insufficient authentication scopes 中创建类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49145661/

相关文章:

android - 有没有可以扫描BLE信标的可穿戴设备?

google-classroom - 如何自定义 Google 类分享按钮的外观

google-api - Google Classroom API 修改附件

android - 方法解析中的歧义以及如何解决它

android - 如何收听哪个应用程序已被选为启动器应用程序?

java - 如何使用 Eclipse 内存分析器工具 (MAT) 分析 HashMap

google-apps-script - 在 Google Classroom API 查询中列出类(class)中 30 名以上的学生

google-classroom - Google Classroom Share 按钮突然不起作用

google-apps-script - 在 Google Classroom API 中发布带有学生副本的类(class)作业时出现内部错误 可能的错误?

android - 对于任何具有 2 个免费/付费版本的应用程序,应用程序商店端的完美场景是什么