LoginSignup
jsbrs125
@jsbrs125 (な は)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

heroku+pythonでGoogleDriveにファイルをアップ&ダウンロードしたいです!

初めまして。最近、プログラミングにハマりましたが、早速つまづいたので、教えていただきたいです。
情報が不足していましたら、追加しますので、何卒よろしくお願いいたします。

最終目的

heroku+pythonでGoogleDriveにファイルをアップ&ダウンロードしたい
(現状、Google認証で止まっています)

環境

Mac
python
GitHub+heroku
GCP

発生している問題・エラー

ローカル上ではできるのですが、herokuで実行すると、次のエラーが出ます。
```
承認エラー
エラー 400: redirect_uri_mismatch

このアプリは Google の OAuth 2.0 ポリシーを遵守していないため、ログインできません。

このアプリのデベロッパーの方は、Google Cloud Console でリダイレクト URI を登録してください。
```

該当するソースコード

参考サイト:https://zenn.dev/wtkn25/articles/python-googledriveapi-auth
(OauthクライアントIDのアプリの種類は、「その他」がなかったので「ウェブアプリ」にしました。)
```python
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import os
import json

If modifying these scopes, delete the file token.pickle.

SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
# 文字列を辞書型に変換(ローカル環境では、os.environ['JSON']→str(client_secrets.json内の文字列コピペ)
csjson = json.loads(os.environ['JSON'])
print(f'dictであれば辞書型になってる : {type(csjson)}')

# ファイルに出力
with open('client_secrets.json','w') as f:
    json.dump(csjson,f,indent=4)
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
"""ここから"""
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
"""ここまでがポイント"""

# Call the Drive v3 API
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(u'{0} ({1})'.format(item['name'], item['id']))

if name == 'main':
main()
```

自分で試したこと

・リダイレクトURIの見直し
(GCP上+client_secrets.jsonどちらも)
最初

http://localhost:8080/
http://localhost:3000/users/auth/google/callback
https://jsbrstest.herokuapp.com/users/auth/google/callback
→ローカルOK、herokuNG

2回目

(削除)http://localhost:8080/
http://localhost:3000/users/auth/google/callback
https://jsbrstest.herokuapp.com/users/auth/google/callback
→ローカルNG、herokuNG

・いくつかの参考サイトには、クライアントID作成時のアプリの種類をその他にしていましたが、選択肢になく、ウェブアプリケーションにしていたので、デスクトップアプリを作って試す。
→ローカルOK、herokuNG

・flaskで、"hello world!"が表示されるようにする
→変わらず

現状

まずは、ローカルと同じように接続できるようにしたいです。
ほかにも色々試しましたが、うまくいかず。。(何をしたのかはわすれてしまいました。)

webアプリを公開したいわけではなく、個人的なプログラミングの定期実行を行わせるために使いたいです。

全く違う方法でもいいので、ご教示ください。。
よろしくお願いします!

0

No Answers yet.

Your answer might help someone💌