简体   繁体   English

asp.net c#Google api文档-401未经授权

[英]asp.net c# google api Documents - 401 Unauthorized

I have this code 我有这个代码

DocumentsService vService = new DocumentsService("test");
vService.setUserCredentials("vUserName", "vPassword");
RequestSettings vSettings = new RequestSettings("test");
DocumentsRequest vDocReq = new DocumentsRequest(vSettings);
Feed<Document> vFeed = vDocReq.GetEverything();
foreach (Document d in vFeed.Entries) {
  lbxDocumente.Items.Add(d.Title + " " + d.Author);
}

Why do I get this exception? 为什么会出现此异常?

System.Net.WebException: The remote server returned an error: (401) Unauthorized System.Net.WebException:远程服务器返回错误:(401)未经授权

This line is trying to authenticate with the actual strings "vUserName" and "vPassword": 该行尝试使用实际的字符串“ vUserName”和“ vPassword”进行身份验证:

vService.setUserCredentials("vUserName", "vPassword");

Did you mean it to be this instead, in order to use variables which you've initialized elsewhere? 为了使用在其他地方初始化的变量,您是不是要这样?

vService.setUserCredentials(vUserName, vPassword);

(What's with the v prefix, by the way? I generally don't like prefixes like this at all, but I've never even seen v as a prefix before...) (什么是与v前缀,顺便?我一般不喜欢这样的前缀在所有的,但我从来没有见过 v之前作为前缀...)

EDIT: You're also not associating the request with the service anywhere. 编辑:您也没有将请求与服务关联到任何地方。 I've tried this code, and it works fine: 我已经尝试过此代码,并且可以正常工作:

DocumentsService service = new DocumentsService("test");
service.setUserCredentials(user, password);
RequestSettings settings = new RequestSettings("test");
DocumentsRequest docReq = new DocumentsRequest(settings);
docReq.Service = service;
Feed<Document> feed = docReq.GetEverything();
foreach (Document d in feed.Entries) {
  Console.WriteLine(d.Title);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM