Search code examples
c#asp.netoauth-2.0dotnetopenauthgoogle-oauth

Google Webmaster SiteVerificatoin with DotNetOpenOauth


I am developing a system to automate the Google site verification using ASP.Net(C#) and I am using DotNetOpenOauth to do the authentication.

I wonder whether there is a way to supply JSON in the request body with scope "https://www.googleapis.com/auth/siteverification" using DotNetOpenOauth? Because when the site verification is been doing, we have to send following JSON with the request.

(using Google OAuth2)

{
  "site": {
    "type": string,
    "identifier": string
  },
  "verificationMethod": string
}

Solution

  • I have found the answer by myself. I have added following code to send the JSON body with the request.

    WebRequest request = WebRequest.Create("https://www.googleapis.com/siteVerification/v1/token?access_token=" + Uri.EscapeDataString(authState.AccessToken));
    string path = HostingEnvironment.MapPath(@"~/App_Data/SiteVerificatoin.json"); 
    
    
    MemoryStream ms = new MemoryStream();
    FileStream fileStreem = new FileStream(path, FileMode.Open, FileAccess.Read);
    byte[] bytes = new byte[fileStreem.Length];
    fileStreem.Read(bytes, 0, (int)fileStreem.Length);
    ms.Write(bytes, 0, (int)fileStreem.Length);
    
    request.ContentType = "application/json";
    request.Method = "POST";
    request.ContentLength = ms.Length;
    ms.Seek(0, SeekOrigin.Begin);
    using (Stream requestStream = request.GetRequestStream())
    {
         ms.CopyTo(requestStream);
    }         
    
    WebResponse response = request.GetResponse();
    

    If you know any better way to do this, please let me know.