远程服务器返回错误:(403)禁止。错误

时间:2014-07-04 08:07:40

标签: .net post httpwebrequest httpwebresponse google-cloud-print

我正在尝试谷歌云打印。云打印的/ search api用于获取printerid和xsrf令牌。所以即时发布数据并尝试获取响应但每次我发布即时获取远程服务器返回错误:(403)Forbidden .. is it跨域问题还是有什么问题?

     string str= TextBox1.Text;
     var request = (HttpWebRequest)WebRequest.Create("https://www.google.com/cloudprint/search?output=json&clientid=" + str);
     request.Method = "POST";
      SetupWebRequest (request);
      string postData = "This is a test that posts this string to a Web server.";
      byte[] byteArray = Encoding.UTF8.GetBytes (postData);
      request.ContentType = "application/x-www-form-urlencoded";
     request.ContentLength = byteArray.Length;
     Stream dataStream = request.GetRequestStream();
     // Write the data to the request stream.
     dataStream.Write(byteArray, 0, byteArray.Length);
     // Close the Stream object.
     dataStream.Close();
     WebResponse response = request.GetResponse ();

1 个答案:

答案 0 :(得分:1)

您必须传递Authorization: Bearer <access token here>标题才能让Google对您的请求进行身份验证。仅仅在URL中传递clientid参数是不够的。

有关如何获取访问令牌的详细信息,请参阅开发人员指南:

https://developers.google.com/cloud-print/docs/appDevGuide