Search code examples
pythonapipush-notificationhuawei-mobile-serviceshuawei-developers

How to use python to call Huawei Push API to authenticate and send simple messages?


My Huawei device does not show push notifications. I got the developer account and app created in their portal. In the app settings, I got the client id and client secret. I am trying to test sandbox using Python but I could not get the server return access token success. { "sub_error": 12304, "error_description": "invalid client_secret", "error": 1101 }

Why is it invalid client secret? What could be wrong in here?


Solution

  • In this case we need to use App id and app secret in the http client id and client secret field as python:

    import http.client
    
    conn = http.client.HTTPSConnection("oauth-login.cloud.huawei.com")
    payload = 'grant_type=client_credentials&client_id=<app id>&client_secret=<app secret>'
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
    conn.request("POST", "/oauth2/v3/token", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    

    It is kind of swapped… but this is how to do it correctly, in python