我正在创建一个 django 应用程序,它为用户创建日历和谷歌文档文件夹,并使用 API 插入事件和添加文档。几个月前,它运行得很好。现在我正在对我的代码进行重大重构,并且在测试上述组件时,我发现它们不再工作了!当我尝试创建文件夹或日历时,我从 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'}
我的代码看起来像这样 - 基于谷歌文档中的这个示例- (文档代码更短,所以我将粘贴那个,尽管身份验证位实际上是两者共同的功能):
from gdata.auth import OAuthSignatureMethod, OAuthToken, OAuthInputParams
from gdata.calendar.service import CalendarService
from django.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")
异常来自最后一行,API 有什么变化还是只是我?(我打赌这只是我,但我看不到它,因为它在几个月前起作用了)