如何正确使用Google Directory API中的“下一页令牌”

时间:2019-05-08 12:21:20

标签: python google-api

按照此处找到的示例...

https://developers.google.com/admin-sdk/directory/v1/guides/manage-mobile-devices

我尝试从目录中的某个OU抓取chromeos设备,以无限循环浏览前100个设备,因为我知道该OU中有350多个设备。我只能拉动的是前100个设备,它会一直循环下去。

我已验证每个下一次呼叫的“下一页令牌”始终是不同的,并且永远不会是“空”,“无”或“空”等。

import pickle
import os.path
import json
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly']

def main():

## assume proper credentials at this poin in the aCreds variable

    aService = build('admin', 'directory_v1', credentials=aCreds)
    aRequest = aService.chromeosdevices().list(customerId='my_customer', orderBy='serialNumber', projection='FULL', pageToken=None, maxResults=500, sortOrder=None, query=None, fields='nextPageToken,chromeosdevices(macAddress, serialNumber, status, orgUnitPath)', orgUnitPath='/1/1030/Devices')

    aPageToken = "init"

    while aPageToken :
        aResults = aRequest.execute()        
        aRequest.pageToken = aResults['nextPageToken']
        aPageToken = aResults['nextPageToken']

        if aResults:
           aString = str(aResults['chromeosdevices']).replace("'", "\"")
           aDevices = json.loads(aString)
           for aRow in aDevices :
             print(aRow["serialNumber"],aRow["macAddress"],aRow["orgUnitPath"])
             aCount = aCount+1

if __name__ == '__main__':
    main()

因此它将重复打印出前100个设备,如下所示

888LPN2 34415dee00e8 /1/1030/Devices.

我希望在打印出350台设备后,该程序将终止,但它将永远持续下去。

1 个答案:

答案 0 :(得分:0)

在此示例中,每次都需要重新创建请求对象,以便循环将内容更改为类似的内容

SCOPES = ['https://www.googleapis.com/auth/admin.directory.device.chromeos.readonly']

def main():

## assume proper credentials at this poin in the aCreds variable

    service = build('admin', 'directory_v1', credentials=aCreds)

## move request from here to inside of loop and change pagetoken for each creation

    aNextPageToken = "one"
    aPageToken = None

    while aNextPageToken :
        aRequest = service.chromeosdevices().list(customerId='my_customer', orderBy='serialNumber', projection='FULL', pageToken=aPageToken, maxResults=100, sortOrder=None, query=None, fields='nextPageToken,chromeosdevices(macAddress, serialNumber, status, orgUnitPath)', orgUnitPath='/1/1030/Devices')
        aResults = aRequest.execute() 
        aNextPageToken =  None
        if 'nextPageToken' in aResults :
           aPageToken = aResults['nextPageToken']
           aNextPageToken = aResults['nextPageToken']

        if aResults:            
           aString = str(aResults['chromeosdevices']).replace("'", "\"")
           aDevices = json.loads(aString)
           for aRow in aDevices :
             print(aRow["serialNumber"],aRow["macAddress"],aRow["orgUnitPath"])
             aCount = aCount+1

if __name__ == '__main__':
    main()