【问题标题】:Failure to add params to endpoint未能将参数添加到端点
【发布时间】:2020-08-14 23:36:26
【问题描述】:

大家好,在尝试编写一个简单的脚本时,我遇到了一个错误,我已经挠了谷歌的头和我的头,但没有找到解决方案。所以问题是每当我运行这段代码时,我都会得到一个 服务器的响应说'api-key is missing'而不是给我关于我输入的号码的信息我不知道我是否做错了顺便说一句。任何帮助将不胜感激 这是我的代码示例

import requests
list = input('Input Phone Numbers List :')
link = "http://apilayer.net/api/validate"
head = {'User-agent': 'user-agent-here'}
s = requests.session()
session = s.get(link,headers=head)
phone = open(list, 'r')
while True:
    num = phone.readline().replace('\n', '')
    if not num:
        break
    cot = num.strip().split(':')
    send = s.post(link,
    data={'access_key':'1135810505585d6e034f640fbf30a700','number':cot[0]},headers=head,)
    (stats, respond) = (send.status_code, send.text)
    print (stats, respond)

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
  • 此 API 的文档在哪里?也许它需要以不同方式或不同名称的 API-Key。或者它可能在s.get() 中需要它,但你只在s.post() 中使用它
  • numverify.com 上的示例表明它需要带有 params=get() 请求,但您使用 get() 而不使用任何 params= 会产生问题
  • @furas 我用参数尝试了 s.get,当我尝试打印出响应时,我得到了与 200 { "success": false, "error": { "code" : 101, "type": "missing_access_key", "info": "You have not provided an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]" } } 但是当我打印出 url 并在浏览器中运行它时工作得很好
  • 你做了get(... params={...}) 吗?这给了我结果,例如电话+14158586273

标签: python python-requests openstack-python-api


【解决方案1】:

numverify.com 的示例表明它需要 GET 请求,因此它需要 get(..., params=...) 的值,但在开始时(while True 之前)您使用不带任何参数的 get() - 这会产生问题。

您不需要 post() 并且(在大多数 API 中)您不需要标头和 cookie。

import requests

#list = input('Input Phone Numbers List :')

link = "http://apilayer.net/api/validate"

payload = {
    'access_key': '1135810505585d6e034f640fbf30a700',
    'number': '',
}

#phone = open(list, 'r')
phone = ['+14158586273', '+46123456789']

for num in phone:
    num = num.strip()
    if num:
        cot = num.split(':')
        
        payload['number'] = cot[0]
        
        response = requests.get(link, params=payload)
        
        print('status:', response.status_code)
        print('text:', response.text)
        print('---')
        
        data = response.json()
        
        print('number:', data['international_format'])
        print('country:', data['country_name'])
        print('location:', data['location'])
        print('carrier:', data['carrier'])
        print('---')
        
        
        

结果:

status: 200
text: {"valid":true,"number":"14158586273","local_format":"4158586273","international_format":"+14158586273","country_prefix":"+1","country_code":"US","country_name":"United States of America","location":"Novato","carrier":"AT&T Mobility LLC","line_type":"mobile"}
---
number: +14158586273
country: United States of America
location: Novato
carrier: AT&T Mobility LLC
---
status: 200
text: {"valid":true,"number":"46123456789","local_format":"0123456789","international_format":"+46123456789","country_prefix":"+46","country_code":"SE","country_name":"Sweden","location":"Valdemarsvik","carrier":"","line_type":"landline"}
---
number: +46123456789
country: Sweden
location: Valdemarsvik
carrier: 
---

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多