Eric Cartmanez
Eric Cartmanez

Reputation: 11

How to fix 'Access token has insufficient scope' error in PHP with Google API?

The goal is to upload server files to user's google drive.

To achieve this I am using Google server-side SignIn to get offline authentication code which I send to my PHP server app where I am using Google API PHP library to authorize my users.

I have made a pretty simple setup on Google Dev Console Where I've made a project and enabled Google Drive API + added OAuth2 credentials which I use for SignIn and Drive API authentication.

The whole authorization process is working. I can get the actual authorization code from user, authenticate it with client from PHP library and get authentication token.

Now, when I try to actually upload the file to Google Drive i get an error from the library (and the response too):

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "insufficientPermissions",
        "message": "Insufficient Permission: Request had insufficient authentication scopes."
      }
    ],
    "code": 403,
    "message": "Insufficient Permission: Request had insufficient authentication scopes."
  }
}

What scopes am I missing? Why is this not included in error message? Is this even a "correct" error? I have looked for a solution for past few days and it's starting to drive me crazy, especially since I have already successfully been able to upload file to server using this guide. The downside of following this guide is, you force users to go to a generated link, authorize via sign in, then copy and send manually the code to the server via form. Not the acceptable solution for the problem I am trying to solve.

Out of desperation I've went on list of scopes for google api, and just added them all to client object. Didn't help tho.

Does anyone else have same issues as me trying to use this PHP library? All help is appreciated.

Upvotes: 0

Views: 7938

Answers (1)

Eric Cartmanez
Eric Cartmanez

Reputation: 11

Alright, my bad. I was copy pasting solutions from Google API without realizing that I also need to add scope to my sign in:

<script>
    function start() {
        gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
                'scope': 'https://www.googleapis.com/auth/drive',
                'discoveryDocs': [
                    'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest',
                ],
            });
        });
    }
</script>

Figured it out after finally reading this doc.

Upvotes: 1

Related Questions