0

我正在尝试通过我的网络应用程序将文件上传到我的谷歌驱动器。我正在为我的 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 文件并保存它。

现在,每当用户尝试从 Web 应用程序上传时。

它将进入身份验证窗口。现在,当我选择帐户时,它是说:

错误: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)

我无法理解我在哪里做错了。请有人解释一下解决方法。

谢谢

4

3 回答 3

0

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

您可以指定您的端口,如下所示,然后一切都会正常工作。

gflags.DEFINE_multi_int('auth_host_port', [8080, 8090, 9000],.....
)
于 2013-02-13T11:41:30.697 回答
0

Upload你上的最后一堂课

self.redirect("/")

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

于 2013-02-01T13:58:06.237 回答
0

我对 python 库并不熟悉,但是无论哪个调用构造身份验证 URI,它显然http://localhost:8080都在参数中添加了 a,正如您在帖子中看到的那样。所以要么你需要弄清楚如何改变要放入的python库的行为localhost:9000,要么你需要在开发者控制台中更改注册以允许localhost:8080

我发现当我通过开发、暂存和产品化应用程序的方式工作时,我最终在开发控制台中构建了六种不同的重定向。我看不到对它有明显的伤害。

于 2013-02-01T21:47:28.993 回答