如何为 android 应用程序设置回调 URL 以接收来自 API 的 POST 请求?

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

我想将 Withings 智能秤集成到我正在开发的 Android 应用程序中。我正在按照 Withings 开发人员文档 here. For this I need to register my app here 中的入门说明进行操作,它需要一个“回调 URL”。以下是 Withings 为回调提供的详细信息 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.

如何为能够接收 POST 请求的 android 应用程序设置回调 URL?

移动应用程序不能作为回调 URL,因为它没有供 API 服务器回调的静态地址。例如,当您从移动网络移动到家庭 wifi,再到咖啡店时,您的 Android IP 会发生变化。

您需要的是一个介于移动应用程序和 Auth 端点之间的服务器,它可以安全地传送登录令牌。您可以使用 Auth0

等服务找到类似的流程

我正在尝试做类似的事情,下面是一个适合我的解决方案。希望这有帮助。

1) 在 Withings 开发者门户中,将回调 URI 注册为:

https://[yourdomain]/callback

将 [yourdomain] 替换为您的域,例如:

https://testtest.azurewebsites.net/callback

2) 在Android工作室

  • 添加一个Activity,例如"RedirectHereActivity"
  • 在strings.xml中添加您的域,例如:
 <string name="domain">testtest.azurewebsites.net</string>
  • 在 AndroidManifest.xml 中,向 "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>

这样,回调与访问代码一起重定向到此 Activity。