We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesHealth Service KitAccessing Health KitCloud-side Data OpennessDeveloping Your AppOpen Authorization in Huawei Health

Open Authorization in Huawei Health

Open Authorization in Huawei Health

You can access data in the Huawei Health app only after the user has authorized the app to open data to Health Service Kit. (Skip this section if you do not need to access data in the Huawei Health app.)

NOTE

You can enable the open authorization to Health Service Kit in the Huawei Health app in either of the following ways:

  1. Call the API for Querying the Privacy Authorization Status to check whether users have toggled on the Health Kit switch. If not, prompt users to toggle on it as follows:
    • Android users: Open the Huawei Health app, go to Me > Privacy management, and toggle on HUAWEI Health Kit.
    • iPhone users: Open the Huawei Health app, go to Me > Third party services > HUAWEI Health Kit, and touch LINK.
  2. Link Health Service Kit via the HTML5 page provided by the Health Service Kit cloud, as described in this section.
    • Display the page for linking Health Service Kit after users complete the authentication. Note that both the authentication and linking require sign-in with a HUAWEI ID. You can use cookies to avoid repeated sign-in.
    • When users use the manual synchronization feature provided by your app, call the API for Querying the Privacy Authorization Status to check whether users have toggled on the Health Kit switch. If not, display the HTML5 page for linking authorization.

To link Health Service Kit by calling the HTML5 page provided by the Health Service Kit cloud, perform the following:

Redirect the user's browser (or the browser component in the mobile phone or an app) to a URL that will display the HTML5 page.
Collapse
Word wrap
Dark theme
Copy code
  1. https://oauth-login.cloud.huawei.com/oauth2/v3/authorize?response_type=code&client_id=106804381&redirect_uri=https%3A%2F%2Fh5hosting.dbankcdn.com%2Fcch5%2Fhealthkit%2Flink%2Fpages%2FprivacyManage.html&access_type=offline&display=
    touch
    &state=
    zh-cn

You can modify the following parameters:

Expand

Parameter Name

Mandatory

Description

display

No

The value is page for PCs, and touch for mobile devices. The default value is page.

state

No

Text language. American English (en-us) is used by default.

For example, if the app whose client_id is 101489619 wants to access the Huawei Health data after passing the authentication, and display the page in Chinese (zh-cn), the URL that users are redirected to will be:

Collapse
Word wrap
Dark theme
Copy code
  1. https://oauth-login.cloud.huawei.com/oauth2/v3/authorize?response_type=code&client_id=106804381&redirect_uri=https%3A%2F%2Fh5hosting.dbankcdn.com%2Fcch5%2Fhealthkit%2Flink%2Fpages%2FprivacyManage.html&access_type=offline&state=zh-cn
NOTE

client_id is always set to 106804381 and cannot be modified. This parameter is used to control the privacy switch in Huawei Health, and does not support Authentication for your app. Therefore, you can use the default value directly.

During this process, users need to sign in to the HUAWEI ID, and touch LINK when redirected to the connection screen.

Interaction Logic with Your App

This section provides a mechanism to keep you aware of whether users have authorized the Huawei Health app to open data to Health Service Kit, so that you can perform further processing according to the returned authorization result.

Android

Implement interactions between your app and the frontend using the addJavascriptInterface method of WebView.

  1. Inject a Java object to WebView using the WebView.addJavascriptInterface(Object object, "jsToAndroid") method.

    NOTE

    Make sure that the name of the Java object to be injected to the WebView is jsToAndroid, so that its definition is consistent with that in the frontend.

  2. Implement the resultCodeProc(String result) method using the @JavascriptInterface annotation, to receive and process the returned value.

The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. class JsObject {
  2. @JavascriptInterface
  3. public String resultCodeProc(String result) {
  4. // Implement logic processing (you can also close the WebView here) after receiving the return code.
  5. Log("get the link result:" + result);
  6. getActivity().finish();
  7. }
  8. }
  9. webview.getSettings().setJavaScriptEnabled(true);
  10. webView.addJavascriptInterface(new JsObject(), "jsToAndroid");

iOS

In the addScriptMessageHandler:name: method that has implemented WebKit, add code for processing interaction messages after the user touches the LINK button.

  1. When initializing WKUserContentController, register a listener for the message named jsToObj through [WKUserContentController addScriptMessageHandler:self name:@"jsToObj"].

    NOTE

    In case of circular dependency, implement addScriptMessageHandler in the -viewWillAppear: method, and removeScriptMessageHandlerForName in the -viewWillDisappear: method.

  2. When the callback is complete from the frontend, read the message.body message named jsToObj using the -userContentController:didReceiveScriptMessage: method, and use the message as the result returned when the user touches the LINK button.

The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. #import <WebKit/WebKit.h>
  2. @interface WKWebViewWKScriptMessageHandlerController () <WKScriptMessageHandler>
  3. // ! Add ScriptMessageHandler and set name to jsToObj.
  4. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  5. [userContentController addScriptMessageHandler:self name:@"jsToObj"];
  6. // ! Complete the configuration.
  7. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  8. configuration.userContentController = userContentController;
  9. // ! Initialize the WebView.
  10. _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
  11. #pragma mark - WKScriptMessageHandler
  12. // ! This method is called back when WKWebView receives ScriptMessage.
  13. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  14. if ([message.name caseInsensitiveCompare:@"jsToObj"] == NSOrderedSame) {
  15. // ! proce message.body
  16. }
  17. }

Returned Result

Expand

resultCode

Description

0

Huawei Health app authorized to open data to Health Service Kit.

401

Huawei Health app fails to be authorized to open data to Health Service Kit.

Search in Guides
Enter a keyword.