gpt4 book ai didi

android - 无法将 Google Fit OAuth 2.0 链接到 WearOS App

转载 作者:行者123 更新时间:2023-11-29 23:46:54 28 4
gpt4 key购买 nike

我正在尝试使用 Google Fit API。我有一个 WearOS 模拟器连接到我的实际设备 (Samsung Galaxy)。当我运行该应用程序时,我很确定我应该弹出一个屏幕来询问权限,但我没有看到这样的屏幕。我完成了在 Google API 控制台中获取 token 的过程。

public class GoalsListActivity extends WearableActivity {

public static final String TAG = "BasicRecordingApi";
private static final int REQUEST_OAUTH_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.goals_screen_layout);

accessGoogleFit();

// Enables Always-on
setAmbientEnabled();
}




/**
* Check if the user has permissions to talk to Fitness APIs; otherwise authetnciate the user and request required permissions
*/

private void accessGoogleFit() {

FitnessOptions fitnessOptions = getFitnessSignInOptions();

if (!hasOAuthPermission()) {
GoogleSignIn.requestPermissions(this, REQUEST_OAUTH_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
} else {
subscribeToRecordingApi();
}
}

/**
* Create a FitnessOptions instance, declaring the Fit API data types and access required by the app
*
* @return FitnessOptions object
*/


private FitnessOptions getFitnessSignInOptions() {
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
return fitnessOptions;
}


/**
* Check if user's account has OAuth permission to Fit API
*
* @return true if user has OAuth permission
*/


private boolean hasOAuthPermission() {
FitnessOptions fitnessOptions = getFitnessSignInOptions();

//Check if the user has previously granted the necessary data access, and if not, inititate the authroization flow
boolean hasAuthO = GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
return hasAuthO;
}

//If the user does not have necessary data access, inititate the authorization flow
private void requestOAuthPermission() {
FitnessOptions fitnessOptions = getFitnessSignInOptions();
GoogleSignIn.requestPermissions(
this, //our activity
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);

}

/**
* SUBSCRIBE TO FITNESS DATA
* To record data from the sensors, we need to first create a subscription.
* To create a subscription, invoke the Recording API. As soon as the subscription is active, fitness data will start recording
*/


private void subscribeToRecordingApi() {

Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {

Log.i(TAG, "Successfully subscribed!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "There was a problem subscribing");
}
});
}

/**
* Get a list of all active subscriptions
*/

private void getAllActiveSubscriptions() {
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.listSubscriptions(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<List<Subscription>>() {
@Override
public void onSuccess(List<Subscription> subscriptions) {
for (Subscription sc : subscriptions) {
DataType dt = sc.getDataType();
Log.i(TAG, "Active subscription for data type: " + dt.getName());
}
}
});
}


/**
* UNSUBSCRIBE - STOP COLLECTING SENSOR DATA IN THE APP
* Cancels the ACTIVITY_SAMPLE subscription on that {@link DataType}
*/


public void stopRecording() {

final String dataTypeStr = DataType.TYPE_ACTIVITY_SAMPLES.toString();
Log.i(TAG, "Unsubscribing from data type: " + dataTypeStr);

//Invoke the Recording API to unsubscribe from the data type and specify a callback that will check the result
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.unsubscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i("TAG", "Successfully unsubscribed for data type: " + dataTypeStr);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//The subscription was not removed
Log.i(TAG, "Failed to unsubscribe for data type: " + dataTypeStr);
}
});
}


}

代码基本来自enter link description here ,这是谷歌自己的示例代码。我有 API key 。出于某种原因,我无法让它工作。谢谢。

最佳答案

你能把你所有的都换掉吗

GoogleSignIn.getLastSignedInAccount(this)

GoogleSignIn.getAccountForExtension(this, fitnessOptions);

关于android - 无法将 Google Fit OAuth 2.0 链接到 WearOS App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51252795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com