Google 工作表 API Post 404 错误
Google Sheets API Post 404 error
我正在尝试 post 数据以 google 传播 sheet 但我收到 404 错误,验证了我的 sheet 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");
}
}];
在你spreadsheetId
中删除/edit
在 范围
的 E 列后添加一个数字
根据您的后续评论,在解决其他人指出的问题后,您现在因范围不足而失败。为了告诉服务器用户授予了您的应用程序权限,您需要代表用户请求 OAuth 授权。 iOS QuickStart 通过创建一个授权控制器解释了如何做到这一点:
// 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;
}
(quickstart使用了spreadsheets.readonly
作用域,但是我在上面把它改成了spreadsheets
,因为看起来你想修改数据。)
您需要确保正确调用了 createAuthController 方法(并且仅在必要时调用),并且在它完成授权后一切正常进行。快速入门指南对此进行了详细解释。
我正在尝试 post 数据以 google 传播 sheet 但我收到 404 错误,验证了我的 sheet 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");
}
}];
在你spreadsheetId
中删除/edit在 范围
的 E 列后添加一个数字根据您的后续评论,在解决其他人指出的问题后,您现在因范围不足而失败。为了告诉服务器用户授予了您的应用程序权限,您需要代表用户请求 OAuth 授权。 iOS QuickStart 通过创建一个授权控制器解释了如何做到这一点:
// 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;
}
(quickstart使用了spreadsheets.readonly
作用域,但是我在上面把它改成了spreadsheets
,因为看起来你想修改数据。)
您需要确保正确调用了 createAuthController 方法(并且仅在必要时调用),并且在它完成授权后一切正常进行。快速入门指南对此进行了详细解释。