谷歌驱动器api验证了Http请求返回403响应"用户未授予应用程序xxxx对文件xxxxxx的读取访问权限#34;

时间:2016-04-24 14:17:40

标签: python python-2.7 oauth-2.0 google-drive-api

我正在编写一个试图通过Drive Api v3连接到Google驱动器的python脚本

# -*- coding: UTF-8 -*-

from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
import os
import httplib2
import gdata.docs.service
import gdata.docs.client
import gdata.docs.data
from apiclient.discovery import build
import io
from apiclient.http import MediaIoBaseDownload


'''
starrt the Authintication Process for Google APi
'''

FLOW = OAuth2WebServerFlow(client_id="****************************",\
                           client_secret="************************",\
                           scope="https://www.googleapis.com/auth/drive",
                           redirect_uri="urn:ietf:wg:oauth:2.0:oob",
                           user_agent="PythonFileEditor/1.0")

OUT_PATH = os.path.join(os.path.dirname(__file__), 'out')
CREDS_FILE = os.path.join(os.path.dirname(__file__), 'credentials.json')

if not os.path.exists(OUT_PATH):
    os.makedirs(OUT_PATH)

storage = Storage(CREDS_FILE)
credentials = storage.get()

if credentials is None:

    authorize_url = FLOW.step1_get_authorize_url()
    print 'Go to the following link in your browser: ' + authorize_url
    code = raw_input('Enter verification code: ').strip()
    try:
        credentials = FLOW.step2_exchange(code)
    except SystemError:
        print SystemError

    storage.put(credentials)

'''Authintication Process is Over'''

http        = httplib2.Http()
http        = credentials.authorize(http)

auth2tocken = gdata.gauth.OAuth2TokenFromCredentials(credentials)


service = build('drive', 'v3', http=http)

results = service.files().list(\
    pageSize=10,fields="nextPageToken, files(id, name)").execute()

items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:

        print('{0} ({1})'.format(item['name'].encode('utf8'), item['id']))
        if item.get('name')=='TestPY.txt':

            fileId=item['id']
            request = service.files().get_media(fileId=fileId)
            fh = io.FileIO(item['name'])
            downloader =MediaIoBaseDownload(fh,request)
            done = False
            while done is False:
                status, done = downloader.next_chunk()
                print "Download %d%%." % int(status.progress() * 100)   

这个脚本,尝试下载名为" TestPY.txt"的文件。从我的谷歌驱动器到我的本地机器,验证过程似乎很好,因为我可以获取文件名称和ID,但当我尝试下载我的文件时,API抛出此异常

raise HttpError(resp, content, uri=self._uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/0Bw2Q7p_e5q0-SFpGUURMSWptV3M?alt=media returned "The user has not granted the app ************** read access to the file 0Bw2Q7p_e5q0-SFpGUURMSWptV3M.">

我的范围是完全特权的,所以理论上我应该能够下载/上传/读/写任何文件!

任何人都能指出正确的方向吗?

谢谢大家。

2 个答案:

答案 0 :(得分:0)

我想通了,我之前使用过/metadata.readonly标志,而且我没有相应地更新我的cridentials文件。

所以即使我的范围现在更大,驱动器也看不到它,因为它没有反映在我的Json文件中。

答案 1 :(得分:0)

  
    

转到您的文件位置并删除token.pickle文件。

         

重新运行该文件,它将起作用。