Google表格API帖子404错误

时间:2016-08-11 09:43:29

标签: ios google-api google-api-client google-sheets-api

我试图将数据发布到谷歌电子表格但是收到404错误,验证了我的工作表ID并且所有一切看起来都很好但仍然出现404错误,下面是我的代码

NSString *baseUrl = @"https://sheets.googleapis.com/v4/spreadsheets/";
NSString *spreadsheetId = @"MYKEY/edit";
NSString *range = @"Sheet1!A2:E";

baseUrl= [baseUrl stringByAppendingString:spreadsheetId];
baseUrl = [baseUrl stringByAppendingString:@"/values/"];
baseUrl = [baseUrl stringByAppendingString:range];


NSMutableDictionary * params=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"RAW",@"valueInputOption", nil];

NSURL *postURL=[GTLUtilities URLWithString:baseUrl queryParameters:params];

NSLog(@"base url is %@", postURL);


GTLObject * body=[[GTLObject alloc]init];


NSMutableArray * contentArray=[[NSMutableArray alloc]init];
NSMutableArray * titleArray=[[NSMutableArray alloc]initWithObjects:@"Student Name",@"Gender",@"Class Level",@"Home State",@"Major",@"Extracurricular Activity",nil];
NSMutableArray * wheelArray=[[NSMutableArray alloc]initWithObjects:@"TEST",@"TEST",@"50.00",@"100.00",@"2",@"2", nil];

[contentArray addObjectsFromArray:titleArray];
[contentArray addObjectsFromArray:wheelArray];

NSLog(@"content array is %@", contentArray);


NSMutableDictionary * bodyDict=[[NSMutableDictionary alloc]initWithObjectsAndKeys:range,@"range",@"ROWS",@"majorDimension",@"HELLO",@"values", nil];
NSLog(@"body dict %@",bodyDict);


body.JSON=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"range",range,@"majorDimension",@"ROWS",@"values",@"HELLO", nil];


[self.service fetchObjectByUpdatingObject:body forURL:[NSURL URLWithString:baseUrl] completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {

    if (error==nil) {

        NSLog(@"posted content successfully");
    }
}];

2 个答案:

答案 0 :(得分:1)

删除 spreadsheetId

中的 / edit

范围

E 列后添加一个数字

答案 1 :(得分:1)

根据您的后续评论,在解决了其他人指出的问题后,您现在失败的范围不足。为了告知服务器用户授予您的应用程序权限,您需要代表用户请求OAuth授权。 iOS QuickStart通过创建auth控制器来解释如何执行此操作:

// Creates the auth controller for authorizing access to Google Sheets API. - (GTMOAuth2ViewControllerTouch *)createAuthController { GTMOAuth2ViewControllerTouch *authController; // If modifying these scopes, delete your previously saved credentials by // resetting the iOS simulator or uninstall the app. NSArray *scopes = [NSArray arrayWithObjects:@"https://www.googleapis.com/auth/spreadsheets", nil]; authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:[scopes componentsJoinedByString:@" "] clientID:kClientID clientSecret:nil keychainItemName:kKeychainItemName delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; return authController; }

(快速入门使用spreadsheets.readonly范围,但我在上面将其更改为spreadsheets,因为它看起来好像要修改数据。)

您需要确保正确调用createAuthController方法(并且仅在必要时),并且当它完成授权时,事情才能正常流动。快速入门指南详细解释了这一点。