0

我正在嘗試使用ASP.Net開發Google網站驗證系統。還使用Google Explorer(https://developers.google.com/site-verification/v1/webResource/insert)來測試請求方法,如JSON和HTTP請求格式。Google網站驗證API

這就是我發送給Google的內容。

POST https://www.googleapis.com/siteVerification/v1/webResource?verificationMethod=site&key={YOUR_API_KEY} 

Content-Type: application/json 
Authorization: Bearer xxxxxxxxxxxxxxx 
X-JavaScript-User-Agent: Google APIs Explorer 

{ 
"id": "myid", 
"owners": [ 
    "[email protected]" 
], 
"site": { 
    "type": "site", 
    "identifier": "http://www.example.net/" 
} 
} 

我收到了Google的迴應。

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "backendError", 
    "message": "Backend Error" 
    } 
    ], 
    "code": 503, 
    "message": "Backend Error" 
} 
} 

>

IAuthorizationState authorization;   
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (googleClient != null) 
     {         
      if (IsPostBack) 
      {      
       authorization = googleClient.ProcessUserAuthorization(); 
       if (authorization != null) 
       { 
        this.AccessToken = authorization.AccessToken; 

       } 
       else if (this.AccessToken == null) 
       { 
        googleClient.RequestUserAuthorization(scope: new[] { GoogleClient.Scopes.WebMaster.SiteVerification }); 
       } 
      }        
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
      if (authorization != null) 
      { 
       IOWebMasterInsertGraph webMasterInsertGraph = googleClient.RequestForVerification(authorization);    

      } 
    } 

public IOWebMasterInsertGraph RequestForVerification(IAuthorizationState authState) 
     { 
      if ((authState != null) && (authState.AccessToken != null)) 
      { 
       WebRequest request = WebRequest.Create("https://www.googleapis.com/siteVerification/v1/webResource?verificationMethod=site"); 


       string path = HostingEnvironment.MapPath(@"~/App_Data/GoogleInsert.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 (response != null) 
       { 
        Stream responseStream = response.GetResponseStream(); 

        if (responseStream != null) 
        { 
         //return GoogleGraph.Deserialize(responseStream); 
         return WebMasterInsertGraph.Deserialize(responseStream); 
        } 
       } 
      } 
      return null; 
     } 

有誰知道這樣做的原因是什麼?

+0

你能否附上你的代碼?您是否使用.NET客戶端庫(https://code.google.com/p/google-api-dotnet-client/)? – peleyal

+0

我正在嘗試使用Google .Net客戶端庫。但是有很多版本兼容。我正在使用DotNetOpenOauth。但是,我通過https://developers.google.com/site-verification/v1/webResource/insert得到了上述錯誤。無論如何,我也會附上我的編碼。 – Chinthaka

回答

0

我自己找到了答案。

必須將以下內容發送到GoogleApi,而不使用JSON中的ID驗證方法必須是您在獲取令牌時選擇的一個。

POST /siteVerification/v1/webResource?verificationMethod=file HTTP/1.1 Host: www.googleapis.com 
    Content-length: 138 
    Content-type: application/json 
    Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

    { 
     "owners": [ 
      "[email protected]" ],  
     "site": { 
      "identifier": "http://test.sample.com/", 
      "type": "SITE" } 
    }