1
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.tools import run_flow 
from oauth2client.file import Storage 
import requests  

CLIENT_ID = '9453asfasfaksdfh860b1osoiveogstt.apps.googleusercontent.com' 
CLIENT_SECRET = '6gRid8wF7TW8asdfasdftX' 


flow = OAuth2WebServerFlow(client_id=CLIENT_ID, 
          client_secret=CLIENT_SECRET, 
          scope='https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly', 
          redirect_uri='http://example.com/auth_return') 

storage = Storage('creds.data') #token details stored here 
credentials = run_flow(flow, storage) 
tokenhere=credentials.access_token #tokens generated 

#send get request with token generated using requests 
r=requests.get("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.ocm",headers={'Authorization': 'Bearer {token}'.format(token=tokenhere)})  
result=r.json() 

これは私が正常にGoogleで認証してユーザーを取得した方法です。

今私はこれを実行します。確認するGoogleアカウントと許可の同意画面を選択するページが表示されます。gmailで一度認証する方法と生成されたトークンを使用する方法は?

しかし、これを実行するたびに問題が発生します。

次回は、この手順を何度も繰り返す必要はなく、トークンを直接渡すか、保存したものを使用することができます。

これは実際どのように実装されていますか?正確な手掛かりはありません。誰かがそれについて私を導いてください。どのようにしてその部分を行うのですか。今私は認証トークンの確認と取得に成功しました。 EXTRA

:私はする必要があり、私はコンソールに直接トークンを取得しようとしていますので、トークンは異なる場合があります取得していますので、その目的のために一つのモジュールを使用していると私はこれを正しく理解していればこのようにそう

答えて

0

を見る方法ここ内訳

from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.tools import run_flow 
from oauth2client.file import Storage 
import requests 
import os 


CLIENT_ID = '9453asfasfaksdfh860b1osoiveogstt.apps.googleusercontent.com' 
CLIENT_SECRET = '6gRid8wF7TW8asdfasdftX' 

def get_new_token(): 
    flow = OAuth2WebServerFlow(client_id=CLIENT_ID, 
           client_secret=CLIENT_SECRET, 
           scope='https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly', 
           redirect_uri='http://example.com/auth_return') 
    storage = Storage('creds.data') #token details stored here 
    credentials = run_flow(flow, storage) 
    tokenhere=credentials.access_token #tokens generated 
    return tokenhere 

def retrieve_saved_token(): 
    if os.path.exists('creds.data'): 
     with open('creds.data') as creds: 
      tokenhere = creds.read() # Change to reflect how the token data is reflected in your 'creds.data' file 
    return tokenhere 

def request_page(tokenhere): 
    r = requests.get("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com", 
        headers={'Authorization': 'Bearer {token}'.format(token=tokenhere)}) 
    result = r.json() 

try: 
    tokenhere = retrieve_saved_token() 
    request_page(tokenhere) 
except: 
    tokenhere = get_new_token() 
    request_page(tokenhere) 

私はFUNCTにあなたのロジックを移動するだけでより多くのオブジェクト指向されるように

を行っていたもののすべてのコンポーネントです:あなたが探していることが何イオン:

def get_new_token(): 
    flow = OAuth2WebServerFlow(client_id=CLIENT_ID, 
           client_secret=CLIENT_SECRET, 
           scope='https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly', 
           redirect_uri='http://example.com/auth_return') 
    storage = Storage('creds.data') #token details stored here 
    credentials = run_flow(flow, storage) 
    tokenhere=credentials.access_token #tokens generated 
    return tokenhere 

def request_page(tokenhere): 
    r = requests.get("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com", 
        headers={'Authorization': 'Bearer {token}'.format(token=tokenhere)}) 
    result = r.json() 

私はまた、トークンファイルが存在する場合は、保存されたトークンを取得するために、新しい機能を追加しました:

def retrieve_saved_token(): 
    if os.path.exists('creds.data'): 
     with open('creds.data') as creds: 
      tokenhere = creds.read() # Change to reflect how the token data is reflected in your 'creds.data' file 
    return tokenhere 

最後に、我々はロジックが実際に実行される部分を取得します。

最初にファイルからトークンを取得しようとします。ファイルが存在しない場合は例外が発生し、例外ロジックが発生します。 トークンの取得に成功した場合でもtryロジックが書き込まれる方法はキャッチされますが、トークンが期限切れになっている場合、要求ロジックは例外を発生させる必要があります。例外のロジックで

それは私はあなただけしたくないと仮定しています新しいトークンを取得し、そのトークン

try: 
    tokenhere = retrieve_saved_token() 
    request_page(tokenhere) 
except: 
    tokenhere = get_new_token() 
    request_page(tokenhere) 

EDIT 1

でページを要求元のロジックを行いますページを要求するだけでなく、ページ上のデータを操作することもできます。これを行うには、作成したjsonオブジェクト 'result'内のタグを探して、ページからデータを取り出す別の関数が必要になります。私はこの関数を以下のコードでdo_something_with_request_function()と呼んでいます。

最初のTry/Exceptは、トークンファイルが存在するかどうかをチェックし、トークンファイルを取得して使用する場合はチェックし、使用しない場合は新しいトークンを生成します。

2回目の試行を除いて、期限切れのトークンを渡す機会があります。そこで、2つのシナリオを想定します。
シナリオ1:有効なトークンを渡すと、要求に必要なページが表示されます。ページ上のデータを解析するために使用するメソッドは、そのページに存在する特定のフィールドを探しますが、すべて正常に動作します。
シナリオ2:無効なトークンを渡すと、期限切れ/無効なトークンのエラーページが表示されます。そのページをjsonに解析してデータを操作しようとするメソッドは、実際に取得しようとしている実際のページに通常存在する値が見つからないと、エラーをスローします。これにより、コードはexceptブロックにジャンプします。
exceptブロックは新しいトークンを生成し、それを渡してページを再度要求し、データを適切に処理します。

try: 
    tokenhere = retrieve_saved_token() 
except: 
    tokenhere = get_new_token() 
try: 
    request_page(tokenhere) 
    do_something_with_request_function(result) 
except: 
    tokenhere = get_new_token() 
    request_page(tokenhere) 
    do_something_with_request_function(result) 
+0

このように、保存された場所からトークンを取得することがすべてです。しかし、トークンについてはどうすればそれを処理するのが正しいか、そのために何ができるのですか? –

+0

コメントを使ってロジックを変更しました。新しいコードは** EDIT 1 **にリストされています。トークンが失効した場合のシナリオを処理する必要があります。 – HackerShark

+0

これを再認証する必要があります。リフレッシュtokeを使用しないと、新しいトークンを生成するということは、一度やり直したユーザーを再び再認証することを意味します。右 –

関連する問題