Reputation: 143
I am making a call to the google indexing API for job postings:
private $client;
private $httpClient;
public function initClient($kernel)
{
$this->client = new \Google_Client();
$this->client->setAuthConfig(JSON_KEY_HERE);
$this->client->addScope('https://www.googleapis.com/auth/indexing');
$this->httpClient = $this->client->authorize();
}
public function sendJob()
{
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
$content = "{
\"url\": \"URL_HERE\",
\"type\": \"URL_UPDATED\"
}";
$response = $this->httpClient->post($endpoint, array('body' => $content));
}
When making the call to the API, the response given is '403 - Forbidden' .
Any ideas what this error actually means? I have created the service account correctly but cannot replicate success from my dev enviroment.
Upvotes: 8
Views: 7569
Reputation: 1153
In my case, the problem was the IP.
So I had this issue, then I fixed it using Ownership like other suggestion from top, which right now in 2023, you should go to Settings>Users And Permissions
to add new ownership for every property (if you have multiple).
Then a few days before, due to having concern about my resources and etc. I've moved my project to a VPS, and then issue started to happen again.
I've cloned the project and tested in shared host from before and was working, but it wouldn't on new VPS.
The thing is that both, Shared host and VPS was from IRAN, since my project is for Iranian users, and after checking the services from both datacenters, I've realized the company that give me the shared host, they've installed something like anti-restriction, so our project in that shared host, can access and do any request to restricted websites such as google, which in some cases, their services are blocked in my country by themselves, not us.
Then again in VPS, when I've figure out what problem could it be, I've tried to use those anti-restriction on VPS, and the result wasn't working, which it SHOULD, since I've used them in my multiple computers and etc. they always work.
Then I came to the last thing that I've afraid of, and it was the blocked IP, for some reason...
I might not be accurate at this point, but it was my last guess, I can use firebase to send message, but I can't use google-indexing-api
.
Right now I'm using the shared host from the datacenter who apply these anti-restriction on network, and send the requests to google using this host.
Hope this can help.
Upvotes: 0
Reputation: 137
I struggled with this today the whole day... the solution was that i forgot to activate the indexer api in the google cloud console.
Upvotes: 2
Reputation: 169
After couple days of headache, here is the answer. Open your Google search console.
Prerequisite: You must already have a google service account
click the tri dot button, and click manage property owners:
add google service account email as new owners:
Upvotes: 15
Reputation: 161
Adding to @Glennstar's comment and @JMs reply there is no longer the old version link (perhaps I never had the old version) but if you click the 3 vertical dots to the right of user owner '(you)' and select 'manage property owners' then on the next page click each of the 'verification details' links to the right of your domain variations (example.com, http://www.example.com, https://www.example.com etc or whatever you have) and add the owner ie paste in the gserviceaccount.com email address from the json it will then say ownership delegated by current owner to that email address as well.
Once status of that email address had changed from 'full' permission to 'owner' back on the search console page I could then run the call fine and the original 403 error about being unable to verify URL ownership was gone. Thanks guys, would have been clueless without this.
Upvotes: 5
Reputation: 688
"Make sure that you have added the service account as an Owner in Google Search Console"
Yep. Use the following code snippet to examine the body response for a more detailed error msg.
var body = result.Content.ReadAsStringAsync().Result;
if the error msg looks like the following, then this is the same problem.
"message": "Permission denied. Failed to verify the URL ownership."
Worth noting: The new version of the search console is lacking. At the bottom of the left-hand menu is a "go to the old version" link. Click this and then select "verification details" from the gear menu (upper right). Next, click on the link that says "verification details" (seriously!). Here, you will finally see a list of verified owners at the bottom of the page. You can add a new owner here, using the email address of your service account (addr can also be found in your json key file).
Upvotes: 10
Reputation: 778
Make sure that you have added the service account as an Owner in Google Search Console as described here: https://developers.google.com/search/apis/indexing-api/v3/prereqs#verify-site.
My problem was that we had multiple entries for the domain in the Search Console (with and without www and with and without https); after adding the service account as an owner to all 4 entries it's working.
Upvotes: 4