从django-python访问Google驱动器

时间:2018-08-09 15:46:56

标签: python django google-oauth2

Django中,我使用社交身份验证应用程序实现了社交登录,然后按照this link进行了配置。 Goolge OAuth运行良好; google-oauth2访问令牌存储在额外的数据字段中。

现在,我想使用此访问令牌列出Google驱动器文件。我尝试过了。

def drive(request):
    user = request.user
    social = user.social_auth.get(provider='google-oauth2')
    response = requests.get(
        'https://www.googleapis.com/auth/drive.metadata.readonly',
        params={'access_token': social.extra_data['access_token']})
    print(response)
    return render(request, 'home/drive.html', {'checking':response})

我收到200的回复,但是我不知道如何列出文件。

我正在使用django 2.0.3python 3.5

1 个答案:

答案 0 :(得分:0)

将设置更改为“重新提示Google OAuth2用户以刷新refresh_token”

SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {     'access_type':'离线' }

并使用google-auth库对Google API进行身份验证

    value = my_frame["A"]
    for i in range(0,len(value)-1):
        if(value[i] == value[i+1]):        
            if value[i] not in my_dic:            
                my_dic[value[i]] = 1
            else:
                my_dic[value[i]] += 1
    else:
        for key,val in my_dic.items():
            print("Element " key," = ",val, " times ")

参考文献:

  1. https://developers.google.com/api-client-library/python/auth/web-app
  2. https://google-auth.readthedocs.io/en/latest/user-guide.html
  3. https://python-social-auth-docs.readthedocs.io/en/latest/use_cases.html