Google.Apis.Fitness.v1 .NET库:权限不足[403]错误

问题描述 投票:0回答:2

我正在尝试使用Google.Apis.Fitness.v1 .NET库,如下所示,并在示例的最后一行(聚合请求执行)上获得权限错误。我为我的项目启用了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();
c# google-api-dotnet-client
2个回答
0
投票

好的..原来是本地机器上缓存的auth-response的问题。看起来在这个应用程序下,我经历了一段时间的身份验证授权流程,并将内容存储在“Google.Fitness.Auth”文件夹中。当时大多数情况下我只是尝试使用这些东西,范围列表并不合适。

后来我按照我想要的方式更改了范围列表。但它没有重新调用身份验证过程。并且事情继续给出旧缓存的auth-response错误。

现在我刚刚删除了“Google.Fitness.Auth”文件夹,然后上面提到的示例工作正常。

所以答案是:删除商店文件夹,然后重试。

但有人可以解释如果APP更改其范围列表会发生什么?我希望auth-process再次触发。但看起来,它不会发生。


0
投票

您似乎没有在Google Developers Console中启用API。尝试使用您的项目名称跟随此链接:https://console.cloud.google.com/project/YOUR_PROJECT_NAME_HERE/apiui/apiview/fitness/overview

© www.soinside.com 2019 - 2024. All rights reserved.