简体   繁体   中英

How to setup a callback URL for an android application to receive POST requests from an API?

I would like to integrate a Withings smart scale into an Android app I am developing. I am following the getting started instructions in the withings developer documentation here . For this I need to register my app here and it requires a "Callback URL". Here are the details Withings provides for the Callback URL:

Partner URL called by our system to send notifications through HTTP POST requests. Make sure that your server can handle a HTTP HEAD request called to verify the validity of your url.

Your URL must :

  • be a valid URL, provided as a URL-encoded string. Please refer to w3schools URL encoding reference to learn more about URL encoding.

  • not be greater than 255 characters.

  • neither contain an IP or 'localhost'. Only port 80 and 443 are allowed.

How do I setup a callback URL for an android app that will be able to receive POST requests?

I am trying to do something similar and below is a solution that works fine for me. Hope this helps.

1) In Withings developer portal, register Callback URI as:

https://[yourdomain]/callback

Replace [yourdomain] with your domain, for example:

https://testtest.azurewebsites.net/callback

2) In Android Studio

  • Add an Activity, for example "RedirectHereActivity"
  • In strings.xml, add your domain, for example:
 <string name="domain">testtest.azurewebsites.net</string>
  • In AndroidManifest.xml, add an intent filter to the "RedirectHereActivity"
<activity android:name=".RedirectHereActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="@string/domain"
            android:pathPrefix="/callback"
            android:scheme="https"/>
    </intent-filter>
</activity>

This way, the callback redirects to this Activity together with the access code.

A mobile application cannot be a callback URL because it has no static address for the API server to callback to. For example, your Android IP changes when you move from a mobile network, to your home wifi, to a coffeeshop.

What you need is a server in-between the mobile app and the Auth endpoint that can securely communicate login tokens. You can find a similar flow using a service like Auth0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM