OAuth authentication fails for google data APIs for hosted domains using gdata
I'm creating a django app that creates a calendar and a google docs folder for the users, and uses the API to insert events and add documents. A few months ago, it worked nice enough; now I'm doing a major refactoring of my code and, while testing the aforementioned components, I discovered that they don't work anymore! When I try to create a folder or a calendar, i get this response from the API:
RequestError: {'status': 401, 'body': '<HTML>\n<HEAD>\n<TITLE>Unknown authorization header</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unknown authorization header</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n', 'reason': 'Unknown authorization header'}
My code looks like this -based on this example in the google documentation- (the docs code is shorter, so I'll paste that one, though the authentication bit is actually a function common to both):
from gdata.auth import OAuthSignatureMethod, OAuthToken, OAuthInputParams
from gdata.calendar.service import CalendarService
from dj开发者_开发百科ango.conf import settings
client_instance = CalendarService()
client_instance.SetOAuthInputParameters(OAuthSignatureMethod.HMAC_SHA1,
settings.OAUTH_CONSUMER_KEY,
consumer_secret=settings.OAUTH_CONSUMER_SECRET)
user_tokens = UserToken.objects.get(user=user)
if not user_tokens.oauth_access_token_value or not user_tokens.oauth_valid_token:
raise Exception('The user has not allowed us to access his services')
oauth_token=OAuthToken(key=user_tokens.oauth_access_token_value, secret=user_tokens.oauth_access_token_secret)
oauth_token.oauth_input_params = OAuthInputParams(OAuthSignatureMethod.HMAC_SHA1,
settings.OAUTH_CONSUMER_KEY,
consumer_secret=settings.OAUTH_CONSUMER_SECRET)
client_instance.SetOAuthToken(oauth_token)
new_folder = docs_service.CreateFolder("some folder")
The exception comes from that last line, has something changed in the API or it's just me? (I bet it's just me, but I can't see it since it worked a couple of months ago)
The reason is layed out in the json response: 'reason': 'Unknown authorization header'
One possible cause is your machine's local time. Make sure that the time on your local machine is correct, as oauth uses the current unix timestamp to sign and verify requests. Google support forum
Here's a list of bugs logged. The first one seems to fit your case well:
- YouTube InsertVideoEntry fails with "Unknown authorization header
- Unknown authorization header, unauthorized
- ValueError after succesful account authentication
Hope this helps :)
精彩评论