在 Google 个联系人中创建一条记录
Create a record in Google contacts
我正在尝试发送一个 post 请求以在我的应用程序的 Google 联系人中创建一条记录,并且我正在发送请求正文中的 "name" 字段,如下所示。
-(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;
}
我正在尝试发送一个 post 请求以在我的应用程序的 Google 联系人中创建一条记录,并且我正在发送请求正文中的 "name" 字段,如下所示。
-(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;
}