403 Forbidden error in Google Index API Using php - php

I am trying to integrate google index API with my website in order to index my side but i am getting 401 error.
I searched and got this solution for my problem.
Google Indexing API - 403 'Forbidden Response'
But this is also not working in my case since i have verified that i am owner in google search console plus integrating right JSON authentication file.
I am checking index matrics but and getting all number of calls in my console platform.
require_once 'google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('service_account_file.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = '{
"url": "http://example.com/jobs/42",
"type": "URL_UPDATED"
}';
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
any help will be appreciated.
Thank in advance

Related

Google indexing API wrong json file

Hi I am trying to use Google indexing API. I used this tutorial - https://developers.google.com/search/apis/indexing-api/v3/prereqs
I used this php library - different branch - v1-master because I dont use composer so I had to use autoload.php
https://github.com/googleapis/google-api-php-client/tree/master
Here is my code:
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/src/Google/autoload.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/examples/templates/base.php');
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/serviceAccount');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = '{
"url": "http://example.com/jobs/42",
"type": "URL_UPDATED"
}';
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
I created service account
downloaded service account json file
This is how the json looks
added path to json as you can see above
I enabled Indexing API in console.developers.google.com for my project
json loads
But I got error Uncaught Google_Exception: Invalid client secret JSON file.
This topic is related to - google-api-php-client: Invalid client secret JSON file
But i just could not resolve it and I am stuck
Issue solved, according to issue here - https://github.com/googleapis/google-api-php-client/issues/2025#issuecomment-762634717, autoload has been removed and whole package depends on autoloader from composer (if you are using it).
If you are not using it, author pointed to the last usable version with autoloader here
https://github.com/googleapis/google-api-php-client/releases/tag/v2.8.2
Download this version and include only one autoload.php in your code
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/vendor/autoload.php');
Whole code is here
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/vendor/autoload.php');
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/mykey-pre-10b793743427.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = '{
"url": "https://my-site.sk/news.php?ID=157",
"type": "URL_UPDATED"
}';
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
return $status_code;
Also dont forget to add client_email from your json into Search Console owners by clicking on Settings - Users and Permission - click on dots next to you account - click Manage owners - add there new Owner (adding it to users in the previous site is not enough) - you would get 403 code.
If operation successful you would get Code 200, but in Search Console nothing would be changed because you initiated index job, that might run in few hours

How to authenticate google-indexing with a service account on a Drupal site

I'm trying to use the google indexing API on a Drupal site. I keep getting a 403 permission denied error.
I've enabled the API, created a service account, verified the site ownership using a meta tag, set owner permission to the service account and have downloaded the google-api-php-client library to make things easier. I put the php file on the server. When I go to the page, I get a 403 error. Permission denied.
require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';
//Set up service account credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS='. DRUPAL_ROOT . '/myjsonfile.json');
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
//-----USING DEFAULT CREDENTIALS SET BY JSON FILE INSTEAD-----
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = "{
\"url\": \"urlToUpdate\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
Edit: I have also tried using the $client->setAuthConfig() method as shown on googles documentation shown here: Google indexing api
Here is alternate code following google api
require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('myjsonfile.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = "{
\"url\": \"urlToUpdate\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
I've found the answer to my problem a while back, so I'm going to post the answer here. To my ire, the reason it wasn't authenticating, was because I registered the http://url instead of the https://url in the google console. So it did not recognize me as an owner of the site. The code is fine, just quadruple check to make sure you register the service account correctly.

Google Drive API - export as HTML results in 500 Internal Error

I've been using the Google Drive API for several months to fetch the contents of a Google Drive Document that I created and export as HTML to embed within my web application.
Sample code:
$client = new Google_Client();
$client->setApplicationName('My App');
$client->setScopes(Google_Service_Drive::DRIVE_READONLY);
$client->setDeveloperKey(GOOGLE_DRIVE_API_KEY);
$client->setAccessType('offline');
$service = new Google_Service_Drive($client);
$files = $service->files->listFiles([
'q' => "'" . GOOGLE_DRIVE_RN_FOLDER_ID ."' in parents",
'orderBy' => 'createdTime desc',
]);
foreach($files as $file)
{
$response = $service->files->export(
$file->id,
'text/html',
array(
'alt' => 'media'
)
);
$html = $response->getBody()->getContents();
}
This code broke last week and I can't figure out why. The $service->files->listFiles() method works successfully (I can see file names in the response), but the $service->files->export() method returns a 500 Internal Error. Sample response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
I also receive the same error when attempting to access the API URL directly in a web browser: https://www.googleapis.com/drive/v3/files/1-DTp74pyo5nnDyR9cWt_d7oi7OdS7JwAZ6ZrKSQ54p0/export?mimeType=text%2Fhtml&key={my_key}.
I tried re-generating API Keys, creating a new Project and generate a brand new API Key. I continue receiving the same error.
Google has apparently changed it's API and the answer is apparently to use a service account, however I'm still waiting on updates for this.
This may answer your question but I haven't had any luck: Google Drive API's file Export endpoint is failing with API Key authentication?
If you get an answer let us know!

Google Analytics Webmaster API getting insufficientPermissions error code :403

I have created service account and below is my code:
$client = new Google_Client();
$client->setApplicationName("Console_Client");
$client->setAuthConfigFile('xxaax.json');
$scopes = [
//"https://www.googleapis.com/auth/webmasters",
//"https://www.googleapis.com/auth/webmasters.readonly"
Google_Service_Webmasters::WEBMASTERS_READONLY,
Google_Service_Webmasters::WEBMASTERS,
];
$client->setScopes($scopes);
$client->setAccessType('offline');
$service = new Google_Service_Webmasters($client);
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$optParams = array('siteUrl' => 'https://www.test.co/');
$request->startDate = "2018-01-01";
$request->endDate = "2018-01-02";
$results = $service->searchanalytics->query($optParams, $request);
I'm receiving this error message in response:
PHP Fatal error: Uncaught Google_Service_Exception
"reason": "insufficientPermissions"
I have enabled this api in my account and am also able to hit it when trying google API explorer.
Why is this occurring?
For this we need to create service account. This is preferable in the case when you are hitting API from console and you don't have any web UI for OAuth2.0.
For this particular case, there is service account Id (looks like long string email). Copy this and add it to your google account.
Doing above solved my problem.
This step was not clear in google API docs.
Hope this helps someone in future.

Create Listing in Etsy using PHP api not working

I'm creating a listing into Etsy using php oAuth.I'm sending following params to api call "quantity,title,description,price,who_made,when_made,is_supply".
I'm following the document as reference here
This is the process flow
Authenticating with etsy using oAuth.
Getting scopes using this api call https://openapi.etsy.com/v2/oauth/scopes
These are working fine.But while creating listing i'm getting following error.
"Invalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect)"
3.The process I followed for create listing
$url = "https://openapi.etsy.com/v2/listings/";
$params = array('description' => 'thisisdesc', 'price'=>'1.00', 'quantity'=>'2', 'title'=>'thisistitle', 'who_made'=>'collective', 'is_supply'=>'false','when_made'=>'2010_2015', 'category_id' => '69105467','shipping_template_id' => '110906760173');
$data = $oauth->fetch($url, $params , OAUTH_HTTP_METHOD_POST);
$json = $oauth->getLastResponse();
print_r($json);
It displays error message.Invalid auth/bad request.
Any ideas/suggestions please.
Change
$url = "https://openapi.etsy.com/v2/listings/";
to
$url = "https://openapi.etsy.com/v2/listings";
The extra / at the end may be causing the issue
Can you try removing the '/' and if possible show the response details.

Categories