0

Web アプリケーションを介して 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ファイルをダウンロードして保存しています。

これで、ユーザーが 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"}))

Googleドライブにアップロードするためのロジックを書いている他のファイルは次のとおりです。

# ヘルプの完全なリンクは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

Uploadクラスの最後にあなたが持っている

self.redirect("/")

これをローカル開発サーバーで実行している場合、開発サーバーのデフォルトのホスト/アドレスであるhttp:// localhost:8080/に何かがあると想定されます。

于 2013-02-01T13:58:06.237 に答える
0

oauth2client.tool を少し変更する必要があります

次のようにポートを指定すると、すべてが正常に機能します。

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

私はPythonライブラリに精通していませんが、どの呼び出しが認証URIを構築していてhttp://localhost:8080も、投稿でわかるように、パラメーターに a を入れているようです。そのため、Python ライブラリの動作を変更しlocalhost:9000localhost:8080.

アプリの開発、ステージング、製品化を進めていると、最終的に開発コンソールに 6 個の異なるリダイレクトが構築されることに気付きました。私が見ることができる明らかな害はありません。

于 2013-02-01T21:47:28.993 に答える