Google.Apis.Fitness.. net库:许可权不足[403]错误
本文关键字:错误 许可权 Fitness Apis net Google | 更新日期: 2023-09-27 18:06:19
我正在尝试使用google . api . fitness。在示例的最后一行出现权限错误(聚合请求执行)。我已经为我的项目启用了Fitness API。
如何解决这个错误?
string clientId = "MY_CLIENT_ID";//From Google Developer console
string clientSecret = "MY_CLIENT_SECRET";//From Google Developer console
string userName = Environment.UserName;
string[] scopes = new string[] {
FitnessService.Scope.FitnessActivityWrite,
FitnessService.Scope.FitnessActivityRead,
FitnessService.Scope.FitnessBodyWrite,
FitnessService.Scope.FitnessBodyRead,
FitnessService.Scope.FitnessLocationRead,
FitnessService.Scope.FitnessLocationWrite
};
UserCredential _userCred = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
},
scopes,
"MY_USER",
CancellationToken.None,
new FileDataStore("Google.Fitness.Auth", false)).Result;
FitnessService _fitnessService = new FitnessService(new BaseClientService.Initializer()
{
HttpClientInitializer = _userCred,
ApplicationName = "Fitness API Use",
});
//Aggregate request
DateTime dt1970 = new DateTime(1970, 1, 1);
AggregateRequest agR = new AggregateRequest();
agR.AggregateBy = new List<AggregateBy>();
AggregateBy oneAgrBy = new AggregateBy();
oneAgrBy.DataSourceId = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps";
oneAgrBy.DataTypeName = "com.google.step_count.delta";
agR.AggregateBy.Add(oneAgrBy);
agR.BucketByActivityType = new BucketByActivity();
agR.BucketByActivityType.MinDurationMillis = 5;
agR.StartTimeMillis = (long)(DateTime.Now.AddHours(-5) - dt1970).TotalMilliseconds;
agR.EndTimeMillis = (long) (DateTime.Now - dt1970).TotalMilliseconds;
UsersResource.DatasetResource.AggregateRequest agRequest = _fitnessService.Users.Dataset.Aggregate(agR, "me");
AggregateResponse agResponse = agRequest.Execute();
OK ..原来是本地机器上缓存auth-response的问题。在这个应用程序下,我经历了一段时间的认证-授权流程,然后东西被存储在"Google.Fitness"中。身份验证"文件夹。大多数时候,我只是在尝试的东西,范围列表是不合适的。
后来我按我想要的方式改变了作用域列表。但它没有重新调用身份验证过程。旧缓存的auth-response总是出错。
我刚刚删除了"Google.Fitness"。Auth"文件夹,然后上面提到的例子工作正常。
所以这里的答案是:删除存储文件夹,然后再试一次。
但是有人能解释一下如果APP改变了它的作用域列表会发生什么吗?我预计授权程序会再次启动。但是看起来,这并没有发生
看起来你没有在Google Developers Console中启用API。尝试用您的项目名称跟随此链接:https://console.cloud.google.com/project/YOUR_PROJECT_NAME_HERE/apiui/apiview/fitness/overview