Trouble making authenticated calls to Google API via OAuth

前端 未结 2 910
傲寒
傲寒 2021-01-24 14:12

When I try to make a call to the Google Directory API using Server to Server authentication, I get the error message \"Not Authorized to access this resource/api\".

What

相关标签:
2条回答
  • 2021-01-24 14:39

    Beyond granting the service account client id access to the given scopes in your Google Apps Control Panel, you need to tell the service account to impersonate a super administrator user within your Google Apps domain:

    $auth->sub = $adminEmail;
    

    For some reason, the Admin SDK docs don't contain a PHP sample but there's sample code for instantiating a service account in the Google Drive docs.

    0 讨论(0)
  • 2021-01-24 14:51

    I found by trial and error that removing "admin." from the scopes makes it work (in addition to everything said above about following these steps: https://developers.google.com/drive/web/delegation#delegate_domain-wide_authority_to_your_service_account ).

    $cs = json_decode(file_get_contents(<MY SECRET PATH> . 'client_secrets.json'), true); 
    $cs = $cs['web'];
    $cred = new Google_Auth_AssertionCredentials(
        $cs['client_email'], //why do they call this "service account name" ? Misleading >:(
        array(
            'https://www.googleapis.com/auth/directory.user',
            'https://www.googleapis.com/auth/directory.group',
            'https://www.googleapis.com/auth/directory.group.member'
        ),
        $key,
        'notasecret',
        'http://oauth.net/grant_type/jwt/1.0/bearer',
        '<MY EMAIL IN THE DOMAIN>' //_my_ email as an user with admin rights
    );
    
    0 讨论(0)
提交回复
热议问题