Google Cloud API - Application Default Credentials

前端 未结 5 1336
借酒劲吻你
借酒劲吻你 2021-02-04 07:04

I have the following code, modified from Google\'s documentation:

        $GOOGLE_APPLICATION_CREDENTIALS = \"./[path].json\";
        $_ENV[\"GOOGLE_APPLICATION         


        
5条回答
  •  长发绾君心
    2021-02-04 07:24

    Use this one it's working for me

    # Includes the autoloader for libraries installed with composer
    require __DIR__ . '/vendor/autoload.php';
    
    putenv('GOOGLE_APPLICATION_CREDENTIALS=../service-account.json');
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    $client->refreshTokenWithAssertion();
    $token = $client->getAccessToken();
    $accessToken = $token['access_token'];
    

提交回复
热议问题