Search code examples
httpgoogle-cloud-platformhttpsgoogle-apigoogle-classroom

Google Classroom API | How add scopes in HTTP request


I want to get a list of courses from an audience by HTTP request, I have set the required areas in the project in Google Cloud, but I still get an error when I try to get the courses.

P.S - Please do not offer me documentation and libraries, do not try to convince me, I just need an HTTP request.

{
    "error": {
        "code": 403,
        "message": "Request had insufficient authentication scopes.",
        "status": "PERMISSION_DENIED",
        "details": [{
            "@type": "type.googleapis.com/google.rpc.ErrorInfo",
            "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
            "domain": "googleapis.com",
            "metadata": {
                "method": "google.classroom.v1.Courses.ListCourses",
                "service": "classroom.googleapis.com"
            }
        }]
    }
}

I tried adding ?scope=https://www.googleapis.com/auth/classroom.courses.readonly to the end of the link Here is the request template

curl \
  'https://classroom.googleapis.com/v1/courses?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

Solution

  • Im going to assume that you are using courses list method

    The call should look something like this in raw HTTP Request

    GET https://classroom.googleapis.com/v1/courses HTTP/1.1
    
    Authorization: Bearer [YOUR_ACCESS_TOKEN]
    Accept: application/json
    

    The access token ([YOUR_ACCESS_TOKEN]) you are sending must be authorized with the proper scope. If you check the documentation page for the method you are using you will see that you should have authorized the user with one of these scopes

    enter image description here

    So in your case the error Request had insufficient authentication scopes. means that when you requested authorization you did not request one of the scopes above. There for your access token has insufficient authorization scopes to make the request.

    The solution is to reauthorize your user with one of the scopes required by the method in question.

    You can read more about how to request authorization and what scopes are in the Using OAuth 2.0 to Access Google APIs documentation page.

    Hint: Your very first in the oauth2 flow contains the scope.

    https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/classroom.courses&response_type=code&redirect_uri=http%3A//127.0.0.1%3A9004&client_id=client_id
    

    This video may help you understand scopes Understanding Google OAuth 2.0 with curl since you appear to be using curl and not just raw HTTP calls.