简体   繁体   English

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

[英]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. 我想将Withings智能秤集成到我正在开发的Android应用中。 I am following the getting started instructions in the withings developer documentation here . 我遵循此处的withings开发人员文档中的入门说明。 For this I need to register my app here and it requires a "Callback URL". 为此,我需要在这里注册我的应用并且需要“回调URL”。 Here are the details Withings provides for the Callback URL: 以下是Withings为回调URL提供的详细信息:

Partner URL called by our system to send notifications through HTTP POST requests. 系统调用的合作伙伴URL,以通过HTTP POST请求发送通知。 Make sure that your server can handle a HTTP HEAD request called to verify the validity of your url. 确保您的服务器可以处理调用的HTTP HEAD请求,以验证您的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. 请参考w3schools URL编码参考以了解有关URL编码的更多信息。

  • not be greater than 255 characters. 不得超过255个字符。

  • neither contain an IP or 'localhost'. 都不包含IP或“ localhost”。 Only port 80 and 443 are allowed. 仅允许端口80和443。

How do I setup a callback URL for an android app that will be able to receive POST requests? 如何为可接收POST请求的Android应用设置回调URL?

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: 1)在Withings开发人员门户中,将回调URI注册为:

https://[yourdomain]/callback

Replace [yourdomain] with your domain, for example: 用您的域替换[yourdomain],例如:

https://testtest.azurewebsites.net/callback

2) In Android Studio 2)在Android Studio中

  • Add an Activity, for example "RedirectHereActivity" 添加一个活动,例如“ RedirectHereActivity”
  • In strings.xml, add your domain, for example: 在strings.xml中,添加您的域,例如:
 <string name="domain">testtest.azurewebsites.net</string>
  • In AndroidManifest.xml, add an intent filter to the "RedirectHereActivity" 在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>

This way, the callback redirects to this Activity together with the access code. 这样,回调将与访问代码一起重定向到此Activity。

A mobile application cannot be a callback URL because it has no static address for the API server to callback to. 移动应用程序不能是回调URL,因为它没有可供API服务器回调的静态地址。 For example, your Android IP changes when you move from a mobile network, to your home wifi, to a coffeeshop. 例如,当您从移动网络转移到家庭wifi,再到咖啡店时,Android IP也会更改。

What you need is a server in-between the mobile app and the Auth endpoint that can securely communicate login tokens. 您需要的是移动应用程序与Auth端点之间的服务器,该服务器可以安全地通信登录令牌。 You can find a similar flow using a service like Auth0 您可以使用Auth0之类的服务找到类似的流程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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