0

I'm trying to authentificate by OAuth token but i didn't find the way to get it without Google prompt and url redirection.

I'm using php and google/apiclient:"^2.7" library

I try to get token with it but I didn't find the way to get it.

If it's not possible to perform it without a google prompt, how I can authenticate my request to the https://logging.googleapis.com/v2/entries:list?

Thanks in advance for your help.

I tried with the following method

public function init()
{

    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $this->container->getParameter('service-account-authentification-file'));
    
    $scopes = [
        'https://www.googleapis.com/auth/cloud-platform',
        'https://www.googleapis.com/auth/cloud-platform.read-only',
        'https://www.googleapis.com/auth/logging.admin',
        'https://www.googleapis.com/auth/logging.read',
        'https://www.googleapis.com/auth/logging.write'
    ];

    $middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
    $stack = HandlerStack::create();
    $stack->push($middleware);
    
    $client = new Client([
        'handler'  => $stack,
        'base_uri' => 'https://logging.googleapis.com/',
        'auth'     => 'google_auth'
    ]);

    $httpClient = $client->authorize();

    $data = [
        "resourceNames" => ["xxxx"],
        "filter"        => "xxxxxx",
    ];

    $response = $httpClient->post('https://logging.googleapis.com/v2/entries:list', [
        'body' => json_encode($data)
    ]);

}

I also tried with this other method provided by the https://github.com/googleapis/google-cloud-php & https://github.com/googleapis/google-cloud-php-logging

    $logging = new LoggingClient([
            'keyFilePath' => $this->container->getParameter('service-account-authentification-file')
        ]
    );

    $entries = $logging->entries([
        'filter' => 'logName = projects/****logs/stdout'
    ]);

    foreach ($entries as $entry) {
        echo $entry->info()['textPayload'] . "\n";
    }

Response is always the same

"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}

I tried to use Curl

curl --request POST \
  'https://logging.googleapis.com/v2/entries:list?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{}' \
  --compressed

but i have such error too

{
  "error": {
    "code": 400,
    "message": "The API Key and the authentication credential are from different projects.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/******/apiui/credential"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CONSUMER_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/******",
          "service": "logging.googleapis.com"
        }
      }
    ]
  }
}

0 Answers0