英文:
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:
<?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>
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 -> {
val postSignInAction = FitActionRequestCode.values()[requestCode]
postSignInAction.let {
performActionForRequestCode(postSignInAction)
}
}
else -> oAuthErrorMsg(requestCode, resultCode)
}
}
Code Request when selecting my 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)
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 将会连接!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论