【问题标题】:Creating Courses in classroom:Request had insufficient authentication scopes在课堂上创建课程:请求的身份验证范围不足
【发布时间】:2018-03-07 06:38:15
【问题描述】:

在谷歌课堂 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 取什么名字,不管这段代码是否正确。

【问题讨论】:

  • 请有人帮助我

标签: android google-classroom


【解决方案1】:

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

授权范围需要以下 OAuth 范围:

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

但您使用的是 :

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

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

【讨论】:

  • 真的很感谢你的解决方案@noogul。我也改变了,直到我得到了。你也说了同样的话。
  • 如何在 Android @noogui 中使用 Google 课堂 API 使用 Oauth 客户端 ID
  • ,我已经创建了身份验证客户端 ID 和所有,现在应用程序基于电子邮件工作,我创建了基于 1 个电子邮件 ID 的 oauth 客户端 ID,每当调用 api 时,它都会询问电子邮件 ID 的错误方式,所以我们需要在编码时提供 id 而不是询问电子邮件
  • 尝试在 SO 中就此提出一个新问题。有更多经验的人可能会提供帮助。请务必包含相关代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 2020-10-29
  • 2021-08-07
  • 2021-01-04
  • 2020-07-07
  • 2021-10-20
相关资源
最近更新 更多