5

I'm trying generate an OAuth access token via the Google .Net GData Client Library. I've been using the instructions in http://code.google.com/apis/gdata/docs/auth/oauth.html as a guide and everything's been working up until I attempt calling OAuthUtil.GetAccessToken( parameters ) at which point I receive a "(400) Bad Request" web exception.

Unfortunately, it's not telling me anything about why it's a bad request. So, I'm kinda baffled about this. The only idea I have is that, because I'm using a localhost url it's not working (though I have registered the url in the Google API Console and it seemed to be okay to do so).

Any ideas what I'm doing wrong here and what I should be doing to make it right?

The OAuthParameters object is constructed like so (sensitive data is censored, hope that doesn't make it hard to answer):

BaseProperties:
    oauth_consumer_key:     <CLIENT_KEY>,
    oauth_nonce:            <NONCE>
    oauth_signature_method: HMAC-SHA1
    oauth_timestamp:        1330440325
    oauth_token:            <TOKEN>
    oauth_verifier:         <VERIFIER>
Callback:        "http://localhost:57381/subscription_auth.aspx?c=google.calendar"
ConsumerKey:     <CLIENT_KEY>
ConsumerSecret:  <CLIENT_SECRET>
ExtraProperties
    oauth_consumer_secret: <CLIENT_SECRET>
    scope:                 https://docs.google.com/calendar/feeds/
    oauth_callback:        http://localhost:57381/subscription_auth.aspx?c=google.calendar
Nonce:           <NONCE>
Scope:           "https://docs.google.com/calendar/feeds/"
Signature:       NULL
SignatureMethod: "HMAC-SHA1"
Timestamp:       "1330440325"
Token:           <TOKEN>
TokenSecret:     NULL  -- No Token Secret was returned from the callback.
Verifier:        <VERIFIER>

This translates into headers for the web response which are like so:

Authorization: OAuth oauth_version="1.0",
oauth_nonce=<NONCE>,
oauth_timestamp="1330441324",
oauth_consumer_key=<CLIENT_KEY>,
oauth_verifier=<VERIFIER>,
oauth_token=<TOKEN>,
oauth_signature_method="HMAC-SHA1",
oauth_signature=<SIGNATURE>

(line breaks added for readability)

NOTE: The above redacted values are encoded correctly where relevant by the GData code.

And the request uri is: https://www.google.com/accounts/OAuthGetAccessToken

The error's happening at line 186 as per the class located here: http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/src/core/oauthutil.cs?r=1123

VVS
  • 19,405
  • 5
  • 46
  • 65
Zac Seth
  • 2,742
  • 5
  • 37
  • 56
  • HTTP response codes for OAuth requests: http://oauth.net/core/1.0a/#http_codes – Jesvin Jose Feb 28 '12 at 17:17
  • Use fiddler to inspect the http response (there should be an error code/description on why you are getting a 400 either in a header or body) or handle the HttpWebException and check the response there. – JoshSchlesinger Mar 01 '12 at 04:09
  • Please show us the actual code that creates the OAuthParameters object and requests the token. – VVS Mar 30 '12 at 08:03
  • Hmm, Unfortunately I never found a solution to this problem - we ended up using the v3 API which came with different client libraries. In my experience, from working with these APIs, the key is using something like Fiddler or Wireshark and inspecting the raw requests. Pay special attention to which parameters are being passed and that they're in the right format (we had problems with date formats, for example). – Zac Seth Apr 02 '12 at 12:26
  • After reading Barry's answer a little closer I noticed that the URL we were using for the SCOPE parameter was somehow a mashup of Google Docs and Google Calendar - that was *probably* the problem. So if you're experiencing similar problems make sure to carefully analyse the values in your parameters. Unfortunately, Google won't tell you exactly what you're doing wrong so it will take a bit of careful investigation on your behalf. – Zac Seth Apr 10 '12 at 11:13

2 Answers2

1

This may be completely unrelated but the URL you have specified in your scope

https://docs.google.com/calendar/feeds/

returns a 404 error

I think you should be using the following instead:

http://www.google.com/calendar/feeds/default/

or possibly http://www.google.com/calendar/feeds/default/owncalendars/full

codingbadger
  • 42,678
  • 13
  • 95
  • 110
  • It's a bit late now but looking at the URL you pointed out, it does seem to be a little off when I think about it (last I checked Google Docs was unrelated to Google Calendar). Anyway, cheers for the reply - it may have been the solution to our problem if we hadn't already taken a different approach... – Zac Seth Apr 10 '12 at 11:09
0

Use real timestamp. It is sensitive to time and locale.

Pavlo Neiman
  • 7,438
  • 3
  • 28
  • 28