我正在尝试使用 ASP.Net 开发 Google 站点验证系统。同样使用谷歌资源管理器(https://developers.google.com/site-verification/v1/webResource/insert)测试JSON和HTTP请求格式等请求方式。
这就是我要发送给 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": [
"development@gmail.com"
],
"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;
}
有谁知道这是什么原因?