Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在谷歌联系人中创建记录_Ios_Objective C_Google Api_Google Contacts Api - Fatal编程技术网

Ios 在谷歌联系人中创建记录

Ios 在谷歌联系人中创建记录,ios,objective-c,google-api,google-contacts-api,Ios,Objective C,Google Api,Google Contacts Api,我正试图发送一个post请求,从我的应用程序在Google contacts中创建一个记录,我正在发送请求正文中的“name”字段,如下所示 -(void)exportGoogleContact:(NSString*)name { CLNetworkDataModel *dataModel = [[CLNetworkDataModel alloc] init]; dataModel.httpMethod = eNetworkOperationTypePost; NSUserDefaults

我正试图发送一个post请求,从我的应用程序在Google contacts中创建一个记录,我正在发送请求正文中的“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;

}

你在关注哪些文档?我在我的问题中解决了上述问题。现在我能够在Google contacts中成功创建该记录。@iVJ你能详细说明一下你是如何解决这个问题的吗,要么用解决方案更新问题,要么你也可以回答你自己的问题。@ViralSavaj只需按照我提供的文档链接,创建与此处所示相同的Json格式,你就会知道,你也可以在此处测试API本身