2017-05-04 114 views
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) 

这里被打破下来是做了什么

只是为了更多的面向对象我搬到你的逻辑到本功能的所有组件离子:

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: 
    tokenhere = retrieve_saved_token() 
    request_page(tokenhere) 
except: 
    tokenhere = get_new_token() 
    request_page(tokenhere) 

编辑1

我假设你不就是想的逻辑请求页面,但也操纵页面上的数据。要做到这一点,你会有另一个功能,通过查找您创建的json对象'result'中的标签来从页面中提取数据。我在下面的代码中调用了这个函数'do_something_with_request_function()'。

第一个Try/Except将检查一个标记文件是否存在,如果它确实抓住该标记并使用它,如果它不存在,则会生成一个新的标记。

第二次尝试除了有机会传递过期的标记。所以我们假设两种情况。
情况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

我改变了与评论的逻辑。新代码在**编辑1 **下列出。这应该处理您的场景,如果令牌过期。 – HackerShark

+0

有了这个我们需要重新授权,如果我们不使用刷新托克,生成新的令牌意味着再次授权用户,他曾经做过。正确 –

相关问题