将文件上传到Google云端硬盘时重定向uri错误

时间:2013-02-01 13:41:21

标签: python google-api google-drive-api tornado google-oauth

我正在尝试通过我的网络应用程序将文件上传到我的google驱动器。  我正在为我的Web应用程序创建客户端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"}))

我正在编写用于上传到Google云端硬盘的逻辑的另一个文件是:

#a帮助完整链接为https://developers.google.com/drive/quickstart-
    蟒#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 :(得分:0)

Upload班级的最后一个

self.redirect("/")

如果你在本地开发服务器上运行它,它希望在http:// localhost:8080 /,这是开发服务器的默认主机/地址。

答案 1 :(得分:0)

我与python库并不亲密,但无论哪个调用正在构建身份验证URI,它显然会在参数中添加http://localhost:8080,正如您在帖子中看到的那样。因此,您需要弄清楚如何更改python库的行为以放入localhost:9000,或者您需要在开发人员控制台中更改注册以允许localhost:8080

我发现,当我通过开发,升级和产品化应用程序时,我最终会在开发控制台中构建六个不同的重定向。我看不到明显的伤害。

答案 2 :(得分:0)

我们需要稍微修改oauth2client.tool

您可以像下面一样指定您的端口,然后每件事都可以正常工作。

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