【问题标题】:how do I use requests for the below API calls?如何使用以下 API 调用的请求?
【发布时间】:2020-01-17 12:13:46
【问题描述】:

下面是我通过 curl 和 python 调用 GitHub api 的代码,我如何停止使用 curl 并使用 requests 库进行 API 调用?我只是对使用请求时调用以下 api 端点的方式感到困惑。

def api_request(self):
        api_key = sys.argv[0]
        organization = sys.argv[1]
        print("------------------------------------------People with 2FA Disabled-----------------------------------------------")
        comm = 'curl -v "https://api.github.com/orgs/"{}"/members?filter=2fa_disabled" -H "Authorization: bearer {}"'.format(organization, api_key)
        os.system(comm)
        print("-----------------------------------------Collaborators being listed----------------------------------------------")
        comm2 = 'curl -v "https://api.github.com/orgs/"{}"/outside_collaborators" -H "Authorization: bearer {}"'.format(organization, api_key)
        os.system(comm2)
        print("-----------------------------------------Members and data Being listed----------------------------------------------")
        comm3 = 'curl -v "https://api.github.com/orgs/"{}"/members" -H "Authorization: bearer {}"'.format(organization, api_key)
        os.system(comm3)

【问题讨论】:

  • 我正在尝试使用请求,但我对使用请求时的外观感到困惑!在我提供的 api url 中,我不知道哪个是标题等...需要有人帮助我
  • 我是网络 API 新手

标签: python python-3.x api github python-requests


【解决方案1】:

import requests
import urllib.request
import io

api_key = sys.argv[0]
organization = sys.argv[1]

url="https://api.github.com/orgs/"{}"/members?filter=2fa_disabled" -H "Authorization: bearer {}".format(organization, api_key)
response = requests.get(url).content

根据答案的格式,您可以尝试

import pandas
df=pandas.read_csv(io.StringIO(response.decode('utf-8')))

【讨论】:

    【解决方案2】:

    我只是对调用以下 api 端点的外观感到困惑 就像使用请求时一样。

    类似:

    import requests
    import sys
    import os
    
    def api_request(self):
        api_key = sys.argv[0]
        organization = sys.argv[1]
        print("------------------------------------------People with 2FA Disabled-----------------------------------------------")
        requests.get("https://api.github.com/orgs/{}/members?filter=2fa_disabled".format(organization),  auth=('bearer', api_key))
    

    【讨论】:

      【解决方案3】:

      以下是可能对您有所帮助的示例:

      responseAPI1 = requests.post('URL',json=data,auth = ('username','password'))
      
      

      或

      headers = {
          'X-ABC-API-ID': 'x-x-x-x-x',
          'X-ABC-API-KEY': 'nnnnnnnnnnnnnnnnnnnnnnn',
          'X-DE-API-ID': 'x',
          'X-DE-API-KEY': 'nnnnnnnnnnnnnnnnnnnnnnnn'
      }
      r = requests.get('https://www.somecompany.com/api/v2/groups/', headers=headers)
      

      【讨论】:

        猜你喜欢
        • 2020-09-28
        • 1970-01-01
        • 2020-05-11
        • 1970-01-01
        • 2023-03-16
        • 2016-01-08
        • 2022-01-13
        • 1970-01-01
        • 2015-10-17
        相关资源
        最近更新 更多