【问题标题】:Google Fit Api No OAuth Result ResponseGoogle Fit Api 无 OAuth 结果响应
【发布时间】:2020-09-05 13:36:51
【问题描述】:

我正在尝试为我的论文项目构建一个 bpm 计数器。我是 Google Fitness Api 的新手,所以我首先从 git 克隆了 google 的计步器示例。 (如果感兴趣,请链接 [此处][1])。我设置了所有内容,授权了我的帐户,将其连接到云项目,使用 keytool 获取 SHA-1 证书,但我仍然无法获得包含我要求的数据的响应,它是 bpm 或步骤。我根据需要在 xml 文件中设置了每个权限,但仍然面临问题。下面,我给你我的代码的一些部分,如果你还需要什么 lmk。提前感谢您的任何回复。

XML 清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

登录功能:

        private fun fitSignIn(requestCode: FitActionRequestCode) {
            if (oAuthPermissionsApproved()) {
                performActionForRequestCode(requestCode)
            } else {
                requestCode.let {
                    GoogleSignIn.requestPermissions(
                            this,
                            requestCode.ordinal,
                            getGoogleAccount(), fitnessOptions)
                }
            }
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
    
            when (resultCode) {
                RESULT_OK -> {
                    val postSignInAction = FitActionRequestCode.values()[requestCode]
                    postSignInAction.let {
                        performActionForRequestCode(postSignInAction)
                    }
                }
                else -> oAuthErrorMsg(requestCode, resultCode)
            }
        }

选择我的 GAccount 时的代码请求:

    private fun performActionForRequestCode(requestCode: FitActionRequestCode) = when (requestCode) {
        FitActionRequestCode.READ_DATA -> readData()
        FitActionRequestCode.SUBSCRIBE -> subscribe()
    }

    private fun oAuthErrorMsg(requestCode: Int, resultCode: Int) {
        val message = """
            There was an error signing into Fit. Check the troubleshooting section of the README
            for potential issues.
            Request code was: $requestCode
            Result code was: $resultCode
        """.trimIndent()
        Log.e(TAG, message)
    }

    private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)
private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)

在请求数据时,我总是以结果 0 结束。

[![模拟器][2]][2]

这是我帐户的云设置。 [![GCloud][3]][3]

我已经从 Google Fit 的常见问题解答中检查了一切是否正确,并且我为我的项目启用了 Fitness Api,但我仍然没有得到任何步骤。如果有人能指出我正确的方向,我将不胜感激。提前致谢 [1]:https://github.com/android/fit-samples/tree/master/StepCounterKotlin [2]:https://i.stack.imgur.com/zvTni.png [3]:https://i.stack.imgur.com/UbtPb.png

【问题讨论】:

    标签: java android kotlin google-api google-oauth


    【解决方案1】:

    回答我自己的问题。经过 4 次痛苦的搜索,解决方案太简单了。 Android Studio 本身在部署调试 apk 时使用 debug.keystore 文件。为了将其更改为您用于签署 SHA-1 的文件。为此,请转到:

    File->Project Structure->Modules->App->Signing Configs 并编辑调试配置以使用您创建的密钥库文件。输入密码和别名并重建您的 apk。这次 Fit Api 将连接!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 2021-01-28
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多