2

I'm working on a tool to access a google spreadsheet via .net. It worked with the old authentication way till google shut them down. Now I reworked to to work with OAuth2 and generated a valid access token.

Issue: even with the access token I get a Bad Request 400 as soon as I try to get the spreadsheet.

Here the code how I setup the service and build the query:

var documentService = new DocumentsService("documents");
var requestFactory = new GDataRequestFactory(null);
requestFactory.CustomHeaders.Add("Authorization: Bearer " + accessToken);
documentService.RequestFactory = requestFactory;

var query = new SpreadsheetQuery { Title = name };
var feed = documentService.Query(uri);

I also tried using the SpreadsheetsService instead as well as adding the access token directly to the query. Then the Uri of the query looks like this:

https://docs.google.com/feeds/default/private/full?category=spreadsheet&title={speadsheetName}&access_token={access_token}

Either way I keep getting the "Bad Request 400" response. Can anyone help me to figure out what goes wrong here?

Thanks!

EDIT: Since to code to access the spreadsheet didn't change by the update to OAuth2 and the access token is valid my best guess is that the service user/developer project misses some permission or something like this. But no idea where to fix that.

EDIT-2: I figured out that the access token works well with new api url's but does not seam to work with old api calls. Unfortunately I can't use the new Google SDK in my environment since I'm restricted to .NET 3.5 or lower. Maybe there is a option to enable the service user to use the old api calls or something like that?

David Speck
  • 438
  • 4
  • 12
  • The usual problem with every single Google service is that it's not liking the source of the request. You may have to allow your app access to the document or service first. – Captain Kenpachi Jun 02 '15 at 13:07
  • I've added the mail address of the service user to "Share" of the spreadsheet and gave it edit permissions. Is that what you mean? – David Speck Jun 02 '15 at 13:17
  • No. I mean Google's API's moan if you suddenly start sending requests from strange clients and IP addresses. For example, if you use your gmail credentials in a sample web application, Google will try to make you fill in a CAPTCHA, but the application obviously doesn't open a browser, so the process fails. – Captain Kenpachi Jun 02 '15 at 14:01
  • How do I allow my app access to the document or needed services? – David Speck Jun 02 '15 at 14:19
  • @JuannStrauss Never seen that once. Are you sure? If you can repeat it, suggest you create an issue and file it with google.(There are issues with 2 step Auth, SMS style auth) – eddyparkinson Jun 03 '15 at 00:03
  • Try this style of Token passing http://stackoverflow.com/questions/15084133/issue-with-oauth2-authentication-with-google-spreadsheet/15152682#15152682 It is java, not sure how to conver to .net – eddyparkinson Jun 03 '15 at 00:05
  • https://productforums.google.com/forum/#!topic/gmail/vtTz1ngyH8o – Captain Kenpachi Jun 03 '15 at 07:23
  • @eddyparkinson I did passed the token already that way. I just forgot to add the content of GetRequestFactory() to the post. Changed that now. – David Speck Jun 03 '15 at 08:21

1 Answers1

2

I found the solution for my problem:

The DocumentsService seam to not work (anymore), at least for me. Using the SpreadsheetsService instead and make sure to use SpreadsheetsService.SpreadsheetQuery instead of DocumentsService.SpreadsheetService solves the issue. The 2nd one was hidden pretty well in my case.

David Speck
  • 438
  • 4
  • 12
  • Documents List API - discontinued on April 20, 2015 http://stackoverflow.com/questions/28169309/do-you-need-to-migrate-to-drive-api-when-using-https-spreadsheets-google-com-f I suspect this was the cause, you should have had a warning email – eddyparkinson Jun 04 '15 at 04:12