For a Dutch employment agency I'm trying to write a bit of code that will use the Google index API to update googles information when a new job is posted or removed. For starters I try to get the notification status, but the response is 404 despite the website obviously being available.
As a starting programmer my conclusion is that I likely made an error within the code somewhere, or that the task at hand is simply impossible since the API is not officially available in my country (The Netherlands)
I unfortunately don't have any ownership of similar sites located in supported countries to assure my code is the problem here.
Searching for topics on this error returns a massive amount of information, none of it so far seems to address my issue sadly enough.
The error appears at the line using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
Below is my code:
using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading.Tasks;
class AccessTokenGenerate
{
public static async Task<string> GetAccessTokenFromJSONKeyAsync(string jsonKeyFilePath, params string[] scopes)
{
using (var stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read))
{
return await GoogleCredential
.FromStream(stream) // Loads key file
.CreateScoped(scopes) // Gathers scopes requested
.UnderlyingCredential // Gets the credentials
.GetAccessTokenForRequestAsync(); // Gets the Access Token
}
}
public static string GetAccessTokenFromJSONKey(string jsonKeyFilePath, params string[] scopes)
{
return GetAccessTokenFromJSONKeyAsync(jsonKeyFilePath, scopes).Result;
}
public static async Task<string> GetTokenAndCall()
{
var keyFilePath = @"E:\LocationOnDisk\KeyFilename.json";
var scopes = new[] { "https://www.googleapis.com/auth/indexing" };
GoogleCredential credential;
using (var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return token;
}
public static string GetToken()
{
var task = GetTokenAndCall();
task.Wait();
var result = task.Result;
return result;
}
}
using System;
using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
var token = AccessTokenGenerate.GetToken();
string responseString = "";
string uri = "https://indexing.googleapis.com/v3/urlNotifications/metadata?url=https%3A%2F%2Fwww.WEBSITEHERE.nl%2F";
WebRequest request = WebRequest.Create(uri);
request.PreAuthenticate = true;
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
responseString = reader.ReadToEnd();
}
Console.WriteLine(responseString);
Console.ReadKey();
}
}
The error 404 indicated that no information regarding the URL has been found. After requesting the URL to be indexed the 404 error also vanished.
Still have got to code to catch the "error" but aside from that it is working now!