apple / app-store-server-library-python

MIT License
147 stars 31 forks source link

test notification request not working (401)in production enviroment requesting #99

Closed 07pepa closed 2 months ago

07pepa commented 2 months ago

even everything is set up correctly and i am requesting sandbox correctly and i have maximal rights on this auth_key i am getting 401 and i am not aware there is need for special auth key for prod here is code

can someone help ?

import time

from appstoreserverlibrary.api_client import AppStoreServerAPIClient, Environment, APIException
from pydantic import BaseModel, Field, SecretBytes, SecretStr
from pydantic_settings import BaseSettings

class TestNotificationToken(BaseModel):
    test_notification_token: str = Field(alias='testNotificationToken')

class Settings(BaseSettings):
    issuer_id: SecretStr
    bundle_id: SecretStr
    key_id: SecretStr 

    class Config:
        extra = 'ignore'

settings = Settings(_env_file='.env.notification_test')
with open('auth_key.p8', 'rb') as f:
    private_key = SecretBytes(f.read())

print(f'this is testing {settings.bundle_id.get_secret_value()}')

client_sandbox = AppStoreServerAPIClient(private_key.get_secret_value(),
                                         settings.key_id.get_secret_value(),
                                         settings.issuer_id.get_secret_value(),
                                         settings.bundle_id.get_secret_value(),
                                         Environment.SANDBOX)

client_prod = AppStoreServerAPIClient(private_key.get_secret_value(),
                                      settings.key_id.get_secret_value(),
                                      settings.issuer_id.get_secret_value(),
                                      settings.bundle_id.get_secret_value(),
                                      Environment.PRODUCTION)

def make_request(client: AppStoreServerAPIClient):
    try:
        token=client.request_test_notification().testNotificationToken
        while True:
            try:
                resp=client.get_test_notification_status(token)
                print(resp.sendAttempts[0].sendAttemptResult.value)
                break
            except APIException as e:
                if e.http_status_code==404:
                    print('waiting for notification')
                    time.sleep(1)
                else:
                    raise e
    except APIException as e:
        print(f"error {e}")

if __name__ == '__main__':
    print("Sending test notification to sandbox")
    make_request(client_sandbox)
    print("testing prod")

this prints

this is testing <REDACTED>
Sending test notification to sandbox
SUCCESS
testing prod
error 401
alexanderjordanbaker commented 2 months ago

@07pepa To confirm, you obtained an API key as specified in https://github.com/apple/app-store-server-library-python?tab=readme-ov-file#obtaining-an-in-app-purchase-key-from-app-store-connect

07pepa commented 2 months ago

yes and it also work for sanbox... just prod is not working...

alexanderjordanbaker commented 2 months ago

Until you have a production release of your app, you will receive 401s from the production API

07pepa commented 2 months ago

ok that make sense closing ( but please put it into documentation )