上传文件到Google Drive时的重定向URI错误

0 投票
3 回答
1623 浏览
提问于 2025-04-17 14:26

我正在尝试通过我的网页应用程序上传文件到谷歌云端硬盘。为了这个网页应用,我创建了一个客户端ID,具体步骤如下:

Client ID:  916885716524-1qvrrridktedn50pasooe1ndepe1oefp.apps.googleusercontent.com
Email address: 916885716524-1qvrrridktedn50pasooe1ndepe1oefp@developer.gserviceaccount.com
Client secret:  6an3xatjgt7sU4Y5v61er7hd
Redirect URIs:  http://localhost:9000/
JavaScript origins: http://localhost:9000/

我下载了一个json文件并保存了它。

现在,每当用户尝试从网页应用上传文件时,系统会跳转到认证窗口。当我选择账户后,它显示:

错误:redirect_uri_mismatch

请求中的重定向URI:http://localhost:8080/ 与注册的重定向URI不匹配

请求详情

from_login=1
scope=https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.apps.readonly https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.file
response_type=code
access_type=offline
redirect_uri=http://localhost:8080/
as=36ff9556bb7c2164
display=page
pli=1
client_id=916885716524-1qvrrridktedn50pasooe1ndepe1oefp.apps.googleusercontent.com
authuser=0
hl=en

如你所见,我在重定向URI中并没有提到8080,但系统还是试图重定向到这个URI。

我的代码如下:

在我的处理程序中:

Class Upload(tornado.web.RequestHandler):
    def post(self, *args, **kwargs):
        # some logic here by which I am getting the file path
        # then calling following function from another file
        file_path = "/home/user/filename.txt"
        upload_to_drive(file_path)
        self.finish(json.dumps({"status": "success"}))

我写上传到谷歌云端硬盘逻辑的另一个文件是:

# 一个有用的链接是 https://developers.google.com/drive/quickstart-
python#step_1_enable_the_drive_api

import os
import sys
import socket
import logging
import httplib2
from mimetypes import guess_type

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
import apiclient
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run

# Log only oauth2client errors
logging.basicConfig(level="ERROR")


token_file = os.path.join(os.path.dirname(__file__), 'sample.dat')

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

# Helpful message to display if the CLIENT_SECRETS file is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to download the client_secrets.json file
and save it at:

   %s

""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)

FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
    scope=[
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/drive.apps.readonly',
      'https://www.googleapis.com/auth/drive.file',
      'https://www.googleapis.com/auth/drive.readonly',
      'https://www.googleapis.com/auth/drive.metadata.readonly',
    ],
    message=MISSING_CLIENT_SECRETS_MESSAGE)


def authorize(token_file, storage):
    if storage is None:
        storage = Storage(token_file)
    credentials = storage.get()

 if credentials is None or credentials.invalid:
    credentials = run(FLOW, storage)

# Create an httplib2.Http object and authorize it with credentials
http = httplib2.Http()

credentials.refresh(http)
http = credentials.authorize(http)
return http


def upload_file(file_path, file_name, mime_type):
# Create Google Drive service instance
    http = httplib2.Http()
    drive_service = build('drive', 'v2', http=http)

    media_body = MediaFileUpload(file_path,
                             mimetype=mime_type,
                             resumable=False)
    body = {
      'title': file_name,
      'description': 'backup',
      'mimeType': mime_type,
    }
    permissions = {
      'role': 'reader',
      'type': 'anyone',
      'value': None,
      'withLink': True
    }

    # Insert a file
    # drive_services.files() is at first an empty list.
    file = drive_service.files().insert(body=body, media_body=media_body).execute()
    # Insert new permissions and create file instance
    drive_service.permissions().insert(fileId=file['id'], body=permissions).execute()
    print 'file uploaded !!'


def file_properties(file_path):
    mime_type = guess_type(file_path)[0]
    file_name = file_path.split('/')[-1]
    return file_name, mime_type

def upload_to_drive(file_path):
    try:
        with open(file_path) as f: pass
    except IOError as e:
        print(e)
        sys.exit(1)

    http = authorize(token_file, None)
    file_name, mime_type = file_properties(file_path)

    upload_file(file_path, file_name, mime_type)

我不太明白我哪里出错了。请有人帮我解释一下解决办法。

谢谢

3 个回答

0

我们需要稍微修改一下oauth2client.tool。

你可以像下面这样指定你的端口,然后一切就会正常工作。

gflags.DEFINE_multi_int('auth_host_port', [8080, 8090, 9000],.....
)
0

Upload类的最后,你会看到

self.redirect("/")

如果你在本地开发服务器上运行这个,它会期待在 http://localhost:8080/ 这个地址能找到一些东西,这是你开发服务器的默认地址。

0

我对这个Python库不太熟悉,但从你发的内容来看,构建认证链接的那部分代码似乎把 http://localhost:8080 放进了参数里。所以,你要么需要找办法去改这个Python库的设置,让它用 localhost:9000,要么就得在开发者控制台里改一下注册设置,允许 localhost:8080

我发现,在开发、测试和上线一个应用的过程中,我的开发者控制台里会积累起好几个不同的重定向链接。看起来并没有什么明显的坏处。

撰写回答