使用条件导出用户 - Python脚本

时间:2018-06-01 21:01:03

标签: python

我的脚本是导出所有用户,但我希望导出type = xyz的用户。目录中有两种类型的用户,例如类型a和类型b,我只想导出类型属性匹配b的用户。

请帮我在脚本中添加一个子句/语句,这样它应该只提取类型为“B”的用户并忽略其他类型的用户。

import requests
import json
import re
import sys
import csv


orgName = ""

apiKey = ""

api_token = "SSWS "+ apiKey

headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}

def GetPaginatedResponse(url):

    response = requests.request("GET", url, headers=headers)

    returnResponseList = []

    responseJSON = json.dumps(response.json())

    responseList = json.loads(responseJSON)

    returnResponseList = returnResponseList + responseList


    if "errorCode" in responseJSON:

        print "\nYou encountered following Error: \n"
        print responseJSON
        print "\n"

        return "Error"

    else:

        headerLink= response.headers["Link"]

        while str(headerLink).find("rel=\"next\"") > -1:

            linkItems = str(headerLink).split(",")

            nextCursorLink = ""
            for link in linkItems:

                if str(link).find("rel=\"next\"") > -1:
                    nextCursorLink = str(link)


            nextLink = str(nextCursorLink.split(";")[0]).strip()
            nextLink = nextLink[1:]
            nextLink = nextLink[:-1]

            url = nextLink

            response = requests.request("GET", url, headers=headers)

            responseJSON = json.dumps(response.json())

            responseList = json.loads(responseJSON)

            returnResponseList = returnResponseList + responseList

            headerLink= response.headers["Link"]


        returnJSON = json.dumps(returnResponseList)

        return returnResponseList



def DownloadSFUsers():

    url = "https://"+orgName+".com/api/v1/users"

    responseJSON = GetPaginatedResponse(url)

    if responseJSON != "Error":


        userFile = open("Only-Okta_Users.csv", "wb")

        writer = csv.writer(userFile)

        writer.writerow(["login","type"]).encode('utf-8')

        for user in responseJSON:


            login = user[u"profile"][u"login"]
            type  = user[u"credentials"][u"provider"][u"type"]
            row = ("+login+","+type).encode('utf-8')

            writer.writerow([login,type])

if __name__ == "__main__":

    DownloadSFUsers()

1 个答案:

答案 0 :(得分:0)

将用户写入cssv文件的语句包含在if语句中,该语句用于测试正确的type