0

I have setup a service account which I am trying to use to access Google Docs API. I keep getting this error

"Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://docs.googleapis.com/v1/documents/1G6BnSgWCEWn6a5UYeF1XjJYuXIwHxwlAmOIqHj3rtgU` resulted in a `401 Unauthorized` response:
{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2  (truncated...)"

I have setup the scopes in my admin google workspace account along with the client id... Below is my code:

<?php
require_once 'vendor/autoload.php';
// use Google\Client;
// use GuzzleHttp\Client;

$client = new Google\Client();
$client->setApplicationName('TESTING GOOGLE DOC API');
$client->setAuthConfig('hale-runner-356311-4acd25674408.json');
// $client->useApplicationDefaultCredentials();
$client->setScopes("https://www.googleapis.com/auth/documents.readonly");
$client->setSubject('generated email when creating service account');
$client->setAccessType('offline');

$guzzle = new GuzzleHttp\Client(['base_uri' => 'https://docs.googleapis.com/v1/documents/']);

$response = $guzzle->request('GET', '1G6BnSgWCEWn6a5UYeF1XjJYuXIwHxwlAmOIqHj3rtgU', [
    'verify' => false,
]);
var_dump($response);

I kind of feel that I need to pass additional data to my request but don't where to put that, have added it after the "verify => false", still getting the same error....

I really need a guide on this.... I will so much appreciate any info.

Thanks

Grokify
  • 15,092
  • 6
  • 60
  • 81
Abhay
  • 27
  • 7
  • Why are you setting it up using the google client library and then suddenly using Guzzle to make a raw HTTP request? That doesn't make a whole lot of sense. The client library contains all the methods you need to do this already, without needing to build your own requests. – ADyson Jul 14 '22 at 13:20
  • Examples in the docs: https://github.com/googleapis/google-api-php-client#authentication-with-service-accounts – ADyson Jul 14 '22 at 13:21
  • thanks.... i didnt go down through the doc...have used it to make the request but am now facing this error "cURL error 60: SSL certificate problem: unable to get local issuer certificate"...how can i bypass this..is there anything similar to "verify" => false – Abhay Jul 14 '22 at 14:11
  • A better solution would be to install the correct root certificate and ensure PHP has access so it. Then you can be sure your connection is secure! – ADyson Jul 14 '22 at 14:13
  • See https://stackoverflow.com/questions/28858351/php-ssl-certificate-error-unable-to-get-local-issuer-certificate – ADyson Jul 14 '22 at 14:14
  • thanks...sorry can i ask you a question outside of this discussion..its on google forms api – Abhay Jul 14 '22 at 14:22
  • If there's something else you need to know, post a new Question, then the whole community can potentially answer :-). Link to it here in the comments too if you want me to look at it specifically. – ADyson Jul 14 '22 at 14:28

0 Answers0