尝试通过 API 调用 Linode 上的资源信息,用默认后台创建的 API Key 发送请求一直提示 token invalid

原因是默认管理后台的 Profile 菜单创建的 API KEY 不是 Personal Access Key,这种密钥应在 https://cloud.linode.com 新版管理后台创建。

Python 调用 Linode API

$(Personal Access Key) 替换成自己创建的 Personal Access Key 即可。

import requests
headers = {'Authorization': 'Bearer $(Personal Access Key)'}
r = requests.get('https://api.linode.com/v4/linode/instances', headers=headers)
return r.text

返回结果示例:

{"pages": 1, "results": 1, "page": 1, "data": [{"ipv6": "2400:8912::f03c:91ff:fe74:4f1c/64", "watchdog_enabled": true, "region": "ap-northeast", "group": "", "ipv4": ["172.115.234.64"], "created": "2018-07-18T00:34:38", "alerts": {"io": 10000, "transfer_quota": 80, "network_in": 10, "network_out": 10, "cpu": 90}, "label": "linode8961401", "image": "linode/ubuntu18.04", "updated": "2018-07-18T01:13:44", "status": "running", "backups": {"schedule": {"window": null, "day": null}, "enabled": false}, "hypervisor": "kvm", "id": 8961401, "specs": {"vcpus": 1, "disk": 25600, "memory": 1024, "transfer": 1000}, "type": "g6-nanode-1"}]}

参考