使用GoogleAPI获取凭证 - HttpResponseRedirect status_code = 302

时间:2018-02-20 12:58:11

标签: python django google-oauth pythonanywhere http-status-code-302

我收到此错误: 的 HttpResponseRedirect'对象没有属性'授权'

我已经打印了HttpResponseRedirect对象,我有类似这样的内容:

HttpResponseRedirect status_code = 302," text / html; charset = utf-8",url =" https://accounts.google.com/o/oauth2/auth?client_id = ..."

我已将此网址复制到我的浏览器中,但确实有效。

当我在本地运行应用程序时一切正常,但现在我最近将我的应用程序移动到了pythonanywhere,它停止了工作。这是我的代码:

def get_credentials(user):

    home_dir = os.path.expanduser('~')
    home_dir = BASE_DIR + '/oauth2/' + user.username
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, 'calendar-python-quickstart.json')
    storage = Storage(credential_path)
    credentials = storage.get()
    if not credentials or credentials.invalid:
        print(True)
        FLOW = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES,redirect_uri="http://XXXXXX.pythonanywhere.com/accounts/google/login/callback/")
        FLOW.params['state'] = xsrfutil.generate_token(SECRET_KEY,
                                                       user)
        authorize_url = FLOW.step1_get_authorize_url()
        return HttpResponseRedirect(authorize_url)
    return credentials


class EventTeamToggleAPI(LoginRequiredMixin, APIView):

    authentication_classes = (authentication.SessionAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        self.slug = self.kwargs.get('slug')
        team = get_object_or_404(Team,slug=self.slug)
        updated = False
        added = False
        user_other_teams = Team.objects.filter(user_team_events=self.request.user).exclude(pk=team.pk)
        games = Game.objects.filter(Q(team_home=team) | Q(team_away=team))
        if self.request.user.google_cal==True:
            credentials = get_credentials(self.request.user)
            http = credentials.authorize(Http())
            service = build('calendar', 'v3', http=http)
            {...}

        return(...)

有人可以向我解释有什么问题吗? 感谢

1 个答案:

答案 0 :(得分:1)

在get_credentials中的一个代码路径中,您将返回一个凭据对象,而在另一个代码路径中,您将返回一个HttpResponseRedirect。