简体   繁体   中英

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

This line is trying to authenticate with the actual strings "vUserName" and "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...)

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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