获取用户DOB&使用Google People API iOS的性别

时间:2018-06-18 07:19:05

标签: ios swift google-people

我在线阅读了多篇文章,但无法找到在iOS应用中集成Google People API的正确文档/步骤。

GTLRPeopleServiceQuery_PeopleGet *getDataQuery = 
[GTLRPeopleServiceQuery_PeopleGet queryWithResourceName: @"people/me"];
    [getDataQuery setPersonFields:@"genders,birthdays"];        

    GTLRPeopleServiceService *gService = [[GTLRPeopleServiceService alloc] init];
    [gService setAPIKey:@"APIKEYHERE"];
    [gService executeQuery: getDataQuery delegate:self didFinishSelector:@selector(getCreatorFromTicket:finishedWithObject:error:)];


-(void)getCreatorFromTicket:(GTLRServiceTicket *) ticket finishedWithObject: (GTLRPeopleService_Person *) finishedWithObject error: (NSError *)error
{
if (error != nil) {
    NSLog(@"Error is %@", error.localizedDescription);
}

NSLog(@"Response is %@", finishedWithObject);
}

我收到以下错误

The request does not have valid authentication credentials.

任何人都可以告诉我,如何添加身份验证凭据或如何解决此问题?

我想要获取用户的性别&出生日期

2 个答案:

答案 0 :(得分:0)

您需要使用OAuth对用户进行身份验证。

我不是iOS工程师,但是您应该查看Google Objective-C客户端代码之一[示例],例如StorageExample StorageSampleWindowController.m。您将需要使用类似于runSigninThenHandler函数的代码,该函数会发出授权请求,然后在服务上设置授权者。

答案 1 :(得分:0)

类型为GIDGoogleUser的经过身份验证的用户对象应作为参数发送到GTLRPeopleServiceService。

@property (nonatomic, strong) GIDGoogleUser *googleUserObject;

-(void)fetchGoogleUserInfo{
    GTLRPeopleServiceQuery_PeopleGet *getDataQuery = [GTLRPeopleServiceQuery_PeopleGet queryWithResourceName: @"people/me"];
    [getDataQuery setPersonFields:@"birthdays,genders"];    
    GTLRPeopleServiceService *gService = [[GTLRPeopleServiceService alloc] init];
    gService.authorizer = [[self.googleUserObject authentication] fetcherAuthorizer];
    [gService executeQuery: getDataQuery delegate:self didFinishSelector:@selector(getCreatorFromTicket:finishedWithObject:error:)];
}


-(void)getCreatorFromTicket:(GTLRServiceTicket *) ticket finishedWithObject: (GTLRPeopleService_Person *) finishedWithObject error: (NSError *)error{
    if (error != nil) {
        DLog(@"Error is %@", error.localizedDescription);
    }
}