ios - 在 Google 通讯录中创建记录

标签 ios objective-c google-api google-contacts-api

我正在尝试发送一个发布请求,以通过我的应用在 Google 通讯录中创建一条记录,并且我将在请求正文中发送“姓名”字段,如下所示。

-(void)exportGoogleContact:(NSString*)name {

CLNetworkDataModel *dataModel = [[CLNetworkDataModel alloc] init];
dataModel.httpMethod = eNetworkOperationTypePost;

NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *token = [data stringForKey:@"Accesstoken"];
NSLog(@"token is %@",token);

dataModel.apiUrl = [NSString stringWithFormat:@"https://people.googleapis.com/v1/people:createContact?access_token=%@",token];
NSString * requestString = [NSString stringWithFormat:@"name=%@",name];

dataModel.requestParams = requestString;
CLNetworkOperation *uuidOperation = [CLNetworkOperation operationForTarget:self callBackSelector:@selector(exportGoogleReponseData:)
                                                                 dataModel:dataModel];

[[CLGlobalNetworkQueue sharedInstance] addOperation:uuidOperation];
  }

我收到以下错误消息:

 {
 "error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"name\": 
  Cannot bind query parameter. Field 'name' could not be found in 
request message.",
"status": "INVALID_ARGUMENT",
"details": [
  {
    "@type": "type.googleapis.com/google.rpc.BadRequest",
    "fieldViolations": [
      {
        "description": "Invalid JSON payload received. Unknown name \"name\": Cannot bind query parameter. Field 'name' could not be found in request message."
      }
    ]
  }
]
         }
 }

任何人都知道如何解决此错误,提前致谢

最佳答案

这是对我来说运行良好的完整代码

 // Export to Google contacts

 -(void)exportGoogleContact:(NSString*)name mobile:(NSString *)mobile phone: 
 (NSString *)phone email:(NSString*)email company:(NSString *)company 
 designation: 
 (NSString *)designation website:(NSString *)website address:(NSString 
 *)address 
 notes:(NSString*)notes {
CLNetworkDataModel *dataModel = [[CLNetworkDataModel alloc] init];
dataModel.httpMethod = eNetworkOperationTypePostJson;
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *token = [data stringForKey:@"Accesstoken"];
NSLog(@"token is %@",token);

NSMutableDictionary * reqDict = [[NSMutableDictionary alloc] init];
[reqDict setObject:name forKey:@"Name"];
[reqDict setObject:mobile forKey:@"MobileValue"];

[reqDict setObject:phone forKey:@"PhoneValue"];

[reqDict setObject:email forKey:@"EmailValue"];
[reqDict setObject:company forKey:@"CompanyValue"];
[reqDict setObject:designation forKey:@"DesignasionValue"];
[reqDict setObject:website forKey:@"WebsiteValue"];
[reqDict setObject:address forKey:@"AddressValue"];

if([notes  isEqual: @""]){

    notes = @"Added through APPName";
}

[reqDict setObject:notes forKey:@"NotesValue"];


    dataModel.apiUrl = [NSString 
stringWithFormat:@"https://people.googleapis.com/v1/people:createContact? 
access_token=%@",token];
//NSString * requestString = [NSString stringWithFormat:@"name=%@",name];
NSString *requestString = [CLUtils createJsonContentForGoogleSync:reqDict];
dataModel.requestParams = requestString;
CLNetworkOperation *uuidOperation = [CLNetworkOperation operationForTarget:self 
callBackSelector:@selector(exportGoogleReponseData:)

dataModel:dataModel];

[[CLGlobalNetworkQueue sharedInstance] addOperation:uuidOperation];
}

//这里是创建json请求的代码

 + (NSString *) createJsonContentForGoogleSync:(NSDictionary *)reqDict{

NSMutableDictionary *phoneDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *phoneDataDict2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *phoneDataDict3 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *phoneDataDict4= [[NSMutableDictionary alloc] init];
NSMutableDictionary *phoneDataDict5 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *phoneDataDict6 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *companyDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *emailDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *designatioDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *websiteDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *nameDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mobileDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *addressDataDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *notesDataDict = [[NSMutableDictionary alloc] init];




[nameDataDict setObject:[reqDict objectForKey:@"Name"] forKey:@"givenName"];
[websiteDataDict setObject:[reqDict objectForKey:@"WebsiteValue"] 
forKey:@"value"];
//[designatioDataDict setObject:[reqDict objectForKey:@"DesignasionValue"] 
forKey:@"value"];
[emailDataDict setObject:[reqDict objectForKey:@"EmailValue"] forKey:@"value"];
[designatioDataDict setObject:[reqDict objectForKey:@"DesignasionValue"] 
forKey:@"title"];
[companyDataDict setObject:[reqDict objectForKey:@"CompanyValue"] 
forKey:@"name"];

[mobileDataDict setObject:[reqDict objectForKey:@"MobileValue"] 
forKey:@"value"];
[mobileDataDict setObject:@"mobile" forKey:@"type"];

[phoneDataDict setObject:[reqDict objectForKey:@"PhoneValue"] forKey:@"value"];
[phoneDataDict setObject:@"work" forKey:@"type"];

NSArray *companyArry;
if(![[companyDataDict objectForKey:@"name"]  isEqual: @""]){
companyArry = [NSArray arrayWithObjects:companyDataDict, 
designatioDataDict,nil];
}

else{

    [companyDataDict setObject:@" " forKey:@"name"];
    companyArry = [NSArray arrayWithObjects:companyDataDict, 
designatioDataDict,nil];

}
NSArray *phoneNosArry;
phoneNosArry = [NSArray arrayWithObjects:mobileDataDict,phoneDataDict,nil];

if([reqDict.allKeys containsObject:@"PhoneValue2"]){
    [phoneDataDict2 setObject:[reqDict objectForKey:@"PhoneValue2"] 
forKey:@"value"];
    [phoneDataDict2 setObject:@"work" forKey:@"type"];
    phoneNosArry = [NSArray 
arrayWithObjects:mobileDataDict,phoneDataDict,phoneDataDict2 ,nil];

}
if([reqDict.allKeys containsObject:@"PhoneValue3"]){

    [phoneDataDict3 setObject:[reqDict objectForKey:@"PhoneValue3"] 
forKey:@"value"];
    [phoneDataDict3 setObject:@"work" forKey:@"type"];
    phoneNosArry = [NSArray 
arrayWithObjects:mobileDataDict,phoneDataDict,phoneDataDict2,phoneDataDict3 
,nil];


    }

    if([reqDict.allKeys containsObject:@"PhoneValue4"]){
        [phoneDataDict4 setObject:[reqDict objectForKey:@"PhoneValue4"] 
forKey:@"value"];
[phoneDataDict4 setObject:@"work" forKey:@"type"];
phoneNosArry = [NSArray 


    }
    if([reqDict.allKeys containsObject:@"PhoneValue5"]){
        [phoneDataDict5 setObject:[reqDict objectForKey:@"PhoneValue5"] 
forKey:@"value"];
        [phoneDataDict5 setObject:@"work" forKey:@"type"];
        phoneNosArry = [NSArray arrayWithObjects:mobileDataDict,phoneDataDict,phoneDataDict2,phoneDataDict3,phoneDataDict4,phoneDataDict5 ,nil];

    }

        if([reqDict.allKeys containsObject:@"PhoneValue6"]){
        [phoneDataDict6 setObject:[reqDict objectForKey:@"PhoneValue6"] forKey:@"value"];
        [phoneDataDict6 setObject:@"work" forKey:@"type"];

            phoneNosArry = [NSArray arrayWithObjects:mobileDataDict,phoneDataDict,phoneDataDict2,phoneDataDict3,phoneDataDict4,phoneDataDict5,phoneDataDict6 ,nil];

        }
//[phoneDataDict addEntriesFromDictionary:mobileDataDict];
[addressDataDict setObject:[reqDict objectForKey:@"AddressValue"] forKey:@"formattedValue"];
[notesDataDict setObject:[reqDict objectForKey:@"NotesValue"] forKey:@"value"];


[notesDataDict setObject:@"TEXT_PLAIN" forKey:@"contentType"];



NSMutableDictionary *parentDict = [[NSMutableDictionary alloc] init];

[parentDict setObject:nameDataDict forKey:@"names"];
[parentDict setObject:emailDataDict forKey:@"emailAddresses"];
[parentDict setObject:companyArry forKey:@"organizations"];
[parentDict setObject:websiteDataDict forKey:@"urls"];
 // [parentDict setObject:designatioDataDict forKey:@"occupations"];
//[parentDict setObject:phoneDataDict forKey:@"phoneNumbers"];
[parentDict setObject:phoneNosArry forKey:@"phoneNumbers"];
//[parentDict setObject:mobileDataDict forKey:@"phoneNumbers"];
[parentDict setObject:addressDataDict forKey:@"addresses"];
[parentDict setObject:notesDataDict forKey:@"biographies"];
NSData *resultData = [NSJSONSerialization dataWithJSONObject:parentDict 
options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString ;
if(resultData != nil){
    jsonString = [[NSString alloc] initWithData:resultData 
encoding:NSUTF8StringEncoding];
}
NSLog(@"####### Final json string is %@",jsonString);
return jsonString;

}

关于ios - 在 Google 通讯录中创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47930868/

相关文章:

java - 在 Google Calendar API 中处理夏令时

android - Google OAuth 身份验证突然失败并出现问题 "disabled_client"

ios - 使用 GPUImage 和 GPUImageHoughTransformLineDetector 检测突出显示的文本边界框

ios - 如何在 Swift 中向后发送 UIView 元素

ios - Swift 中的 AVCaptureVideoPreviewLayer 框架

ios - 中断 UITableView 单元格调整大小动画?

javascript - 如果服务不可用,gapi.client 是否应该优雅地失败?

android - 如何在 Flutter 中对整个应用程序进行圆角处理?

ios - 如何为 UITableView 部分创建自定义背景?

objective-c - 使用 Sparkle Framework 更新时显示 .dmg EULA