简体   繁体   English

如何从此 API 中获取所有用户 email?

[英]How to get all users email from this API?

I am using this API and trying to get all of the user's emails.我正在使用这个 API 并试图获取所有用户的电子邮件。 I tried looping the result, but it just returns the first result looped to the number of users.我尝试循环结果,但它只是返回循环到用户数的第一个结果。

Below is my current code.以下是我当前的代码。

def get_user():

    url = 'https://api.pagerduty.com/users'
    headers = {
        'Accept': 'application/vnd.pagerduty+json;version=2',
        'Authorization': 'Token token={token}'.format(token=API_KEY)
    }

    r = requests.get(url, headers=headers)

    result = []
    emails = r.json()['users'][0]['email']
    for email in emails:
        if r.status_code == 200:
            result.append(emails)
    return result

I am currently returning the result in an appended list, which is not working, so how do I get the email of each user, not just the first one?我目前在附加列表中返回结果,但该列表不起作用,那么如何获取每个用户的 email,而不仅仅是第一个?

You need to loop over the users object and get the email from each user.您需要遍历users object 并从每个用户处获取email Currently, you are only getting the email for user 0.目前,您仅获得用户 0 的 email。

if r.status_code == 200:
    result = [user['email'] for user in r.json()['users']]
else:
    # raise some error, set result = [], or some other indication of failure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM