0

Hello guys while trying to make a simple script i have encountered an error and i've scratched google's head and mine but didn't find a solution . So the problem is anytime i run this code i get a response from the server saying ' the api-key is missing' instead of giving me info on the number i enter i dont know if i'm doing anything wrong btw . Any help would you be appreciated This is a sample of my code

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)
Dan Dan
  • 3
  • 1
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Aug 14 '20 at 23:52
  • where is documentation for this API? Maybe it needs API-Key in different way or with different name. OR maybe it needs it in `s.get()` but you use it only in `s.post()` – furas Aug 14 '20 at 23:54
  • example on https://numverify.com/ shows that it needs `get()` requests with `params=` but you use `get()` without any `params=` and it can makes problem – furas Aug 14 '20 at 23:58
  • @furas I tried s.get with the params and when i tried to print out the response I got the same error code as 200 { "success": false, "error": { "code": 101, "type": "missing_access_key", "info": "You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]" } } But when i print out the url and run it in a browser it works just fine – Dan Dan Aug 15 '20 at 00:04
  • did you do `get(... params={...})` ? this gives me result for example phone `+14158586273` – furas Aug 15 '20 at 00:07
  • @furas yh i did it and when i printed out the url and open it in the browser i got the info but when i try to print the response I get the **missing api-key error** – Dan Dan Aug 15 '20 at 00:12
  • you have `get()` without `params=` before `while True` and it makes problem. You don't need to use `get()` to get cookies. You don't need even headers when you work with API. – furas Aug 15 '20 at 00:15
  • @furus Thanks bro for the info i didnt know that , I'm still learning and i hope i become real good . I will try to fix my errors and see what happens .Once again thanks i really appreciate that – Dan Dan Aug 15 '20 at 00:19

1 Answers1

0

Example on numverify.com shows that it needs GET request so it needs values as get(..., params=...) but at start (before while True) you use get() without any parameters - and it makes problem.

You don't need post() and (as in most API) you don't need headers, and cookies.

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('---')
        
        
        

Result:

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: 
---
furas
  • 134,197
  • 12
  • 106
  • 148