谷歌健康 API 无 OAuth 结果响应

huangapple go评论95阅读模式
英文:

Google Fit Api No OAuth Result Response

问题

以下是翻译好的部分:

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)
    }
}

在选择谷歌账号时的代码请求:

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)

如有需要,请提供更多细节。感谢您提前的任何回复。

英文:

I am trying to build a bpm counter for my thesis project. I am a novice regarding Google Fitness Api, so I started by cloning google's step counter sample from git. (Link if interested here). I set everything up, I authorised my account, connected it to a cloud project, used keytool for an SHA-1 cert, but I still cannot get a Response with the data I ask for, where it is bpm or steps. I set every permission as needed in the xml file and still face the issue. Below, I give you some parts of my code, if you need anything else lmk. Thanks in advance for any responses.

XML Manifest:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.myapplication&quot;&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACTIVITY_RECOGNITION&quot; /&gt;
    &lt;uses-permission android:name=&quot;com.google.android.gms.permission.ACTIVITY_RECOGNITION&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

SignIn Function:

        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 -&gt; {
                    val postSignInAction = FitActionRequestCode.values()[requestCode]
                    postSignInAction.let {
                        performActionForRequestCode(postSignInAction)
                    }
                }
                else -&gt; oAuthErrorMsg(requestCode, resultCode)
            }
        }

Code Request when selecting my GAccount:

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

    private fun oAuthErrorMsg(requestCode: Int, resultCode: Int) {
        val message = &quot;&quot;&quot;
            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
        &quot;&quot;&quot;.trimIndent()
        Log.e(TAG, message)
    }

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

I always end up with a result 0 when requesting data.

[![emulator][2]][2]

And here is my cloud settings for my account.
[![GCloud][3]][3]

I have triple-checked that everything is done correctly from Google Fit's FAQ and that I have the Fitness Api enabled for my project, but I still don't get any steps as a result. I would be grateful if anyone could point me to the right direction. Thanks in advance
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

答案1

得分: 1

回答我自己的问题。经过4天的煎熬般的搜索,解决方案竟然非常简单。在部署调试 APK 时,Android Studio 本身会使用一个 debug.keystore 文件。为了将其更改为用于签署 SHA-1 的文件。操作如下:

> 文件->项目结构->模块->应用->签名配置,然后编辑调试配置以使用你创建的密钥库文件。输入密码和别名,然后重新构建 APK。这次 Fit API 将会连接!

谷歌健康 API 无 OAuth 结果响应

英文:

Answering my own question. After 4 dys of agonizing searching, the solution was far too simple. Android Studio by itself when deploying debug apks, uses a debug.keystore file. In order to change that to the file that you used to sign your SHA-1. To do that, Go to:

> File->Project Structure->Modules->App->Signing Configs and edit the debug config to use your keystore file that you created. Input the password and the alias and rebuild your apk. This time the Fit Api will connect!

谷歌健康 API 无 OAuth 结果响应

huangapple
  • 本文由 发表于 2020年9月5日 21:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63754503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定