paypal API call returns invalid_client

42 Views Asked by At

Hello I am trying to implement my web page developed in flask (python), I have a paypal payment process which is working successfully on visaul studio code, However when I upload my app on lightsail this is not working. This is the code and I recive and except :

def approve_payment(order_id):
    print("approve_payment", order_id)
    try:
        api_link = f"https://api-m.sandbox.paypal.com/v2/checkout/orders/{order_id}/capture"

        headers = {
            "Content-Type": "application/json",
        }
        print("Headers: ", headers)
        print("api_link", api_link)

        response = requests.post(url=api_link, headers=headers, auth=(paypal_client_id, paypal_secret))
        response.raise_for_status()
        json_data = response.json()
        print("json_data", json_data)
        return json_data
    except requests.exceptions.HTTPError as err:
        # Handle HTTP errors

        status_code = err.response.status_code
        response_text = err.response.text
        # Log or print the details for debugging
        print(f"HTTPError: Status Code {status_code}, Response Text: {response_text}")
        return {"error": "HTTPError", "status_code": status_code, "response_text": response_text}

        
    except Exception as e:
        # Handle other exceptions

        return {"error": "Exception", "message": str(e)}

This is the output of print:

HTTPError: Status Code 401, Response Text: {"error":"invalid_client","error_description":"Client Authentication failed"}

However I already confirmed the PAYPAL_CLIENT_ID and paypal_secret and this is correct, for me seems this is a network issue.

I appreciate if someone has any idea which could be the problem Thanks

On lightsail I have the next ports open

Application Protocol Port or range / Code Restricted to
SSH TCP 22 Any IPv4 address

Lightsail browser SSH/RDP DNS (TCP) TCP 53 Any IPv4 address

HTTP TCP 80 Any IPv4 address

HTTPS TCP 443 Any IPv4 address

DNS (UDP) UDP 53 Any IPv4 address

On paypal dashbord I don't see the API calls when those failed Thanks

1

There are 1 best solutions below

0
Diego Emilio Morales Ramírez On

Finally Identify the issue, this was authentication issue, not sure why but the application in flask was not pulling the credentials from .env file, so I sent the request with "None" credentials, I was using:

paypal_client_id = os.environ.get('PAYPAL_CLIENT_ID')

paypal_secret = os.environ.get('PAYPAL_SECRET')

I replace with

paypal_client_id = os.getenv('PAYPAL_CLIENT_ID')

paypal_secret = os.getenv('PAYPAL_SECRET')

this is working now thanks