可以使用基础认证来代替OAuth2进行API调用。需要在请求头中添加Authorization字段,并将其设置为“Basic 'base64-encoded credentials'”,其中'base64-encoded credentials'是使用Base64编码的用户名和密码字符串。示例代码如下:
import requests
import base64
username = 'your_username'
password = 'your_password'
endpoint_url = 'https://example.com/api/followers'
# encode credentials with Base64
credentials = username + ':' + password
encoded_credentials = base64.b64encode(credentials.encode('ascii')).decode('ascii')
# make request with Basic Authentication
headers = {
'Authorization': 'Basic ' + encoded_credentials
}
response = requests.get(endpoint_url, headers=headers)
followers_count = response.json()['followers_count']
print(followers_count)
请确保将实际的用户名,密码和端点URL替换为示例中的值。