xnl96
xnl96

Reputation: 3

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

Upvotes: 0

Views: 521

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500504

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

Upvotes: 1

Related Questions