73

ライブラリ(https://pypi.python.org/pypi/PyDriveGoogleAuth )を使用するときのプロセスを自動化しようとしています。pydrive

動作するように pydrive と Google API をセットアップしましたsecret_client.jsonが、スクリプトを実行するたびに gdrive アクセスに Web 認証が必要です。

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile

drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')

eng.txt単なるテキストファイルです。さらに、別のアカウントにログインしているときに上記のスクリプトを使用しようとすると。を生成した gdrive にアップロードするのではなく、認証を承認したときにログインしたアカウントをアップロードしますeng.txtsecret_client.json

以前の投稿から、検証プロセスを自動化するために次のことを試しましたが、エラー メッセージが表示されます。

import base64, httplib2
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

#gauth = GoogleAuth()
#gauth.LocalWebserverAuth()

# from google API console - convert private key to base64 or load from file
id = "464269119984-j3oh4aj7pd80mjae2sghnua3thaigugu.apps.googleusercontent.com"
key = base64.b64decode('COaV9QUlO1OdqtjMiUS6xEI8')

credentials = SignedJwtAssertionCredentials(id, key, scope='https://www.googleapis.com/auth/drive')
credentials.authorize(httplib2.Http())

gauth = GoogleAuth()
gauth.credentials = credentials

drive = GoogleDrive(gauth)

drive = GoogleDrive(gauth)

textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile

drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')

エラー:

Traceback (most recent call last):
  File "/home/alvas/git/SeedLing/cloudwiki.py", line 29, in <module>
    textfile.Upload()
  File "/usr/local/lib/python2.7/dist-packages/pydrive/files.py", line 216, in Upload
    self._FilesInsert(param=param)
  File "/usr/local/lib/python2.7/dist-packages/pydrive/auth.py", line 53, in _decorated
    self.auth.Authorize()
  File "/usr/local/lib/python2.7/dist-packages/pydrive/auth.py", line 422, in Authorize
    self.service = build('drive', 'v2', http=self.http)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/apiclient/discovery.py", line 192, in build
    resp, content = http.request(requested_url)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 475, in new_request
    self._refresh(request_orig)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 653, in _refresh
    self._do_refresh_request(http_request)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 677, in _do_refresh_request
    body = self._generate_refresh_request_body()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 861, in _generate_refresh_request_body
    assertion = self._generate_assertion()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 977, in _generate_assertion
    private_key, self.private_key_password), payload)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/crypt.py", line 131, in from_string
    pkey = crypto.load_pkcs12(key, password).get_privatekey()
OpenSSL.crypto.Error: [('asn1 encoding routines', 'ASN1_get_object', 'header too long')]

gdrive api での認証は次のようになります。

ここに画像の説明を入力

使用するたびに認証する必要がないように、pydrive を使用するにはどうすればよいですか?

pydrive スクリプトを使用する python スクリプトがsecret_client.json、現在インターネット ブラウザにログオンしているアカウントではなく、生成されたアカウントにのみアップロードするように自動認証を許可するにはどうすればよいですか?

4

5 に答える 5

138

まず、これがどのように機能するかについて、非常に重要な部分を誤解しています。

別のアカウントにログインしているときに上記のスクリプトを使用しようとすると。secret_client.json を生成した gdrive に eng.txt をアップロードするのではなく、認証を承認したときにログインしたアカウントをアップロードします

これはまさにそれが機能するはずの方法です。開発者はclient_secret.jsonアプリケーションと共に配布し、そのファイルは PyDrive によってGoogleでアプリケーションを認証するために使用されます。Google は、あらゆる種類の理由 (指標、アカウントへの課金、アクセスの取り消しなど) で、各アプリケーションによって作成された API リクエストの数を知りたいため、アプリケーション自体を認証する必要があります。

これで、アプリケーションが実行されると、GoogleでクライアントLocalWebserverAuthが認証されます。もちろん、クライアントは実際にアプリケーションを使用する人です。この場合、開発者とクライアントは同じ人 (あなた) ですが、あなたのアプリケーションを何百万人もの人に配布したいと考えているとします。. _client_secret.json

とはいえ、アプリを実行するたびにアプリがクライアントに認証を求める必要がないようにするための変更は、実際には非常に小さな変更にすぎません。LoadCredentialsFileとを使用するだけですSaveCredentialsFile

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile

drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')
于 2014-07-02T23:20:14.720 に答える