Syed Ismail Ahamed
Syed Ismail Ahamed

Reputation: 349

Google Sheets API Post 404 error

Am trying to post data to google spread sheet but am getting 404 error,verified my sheet id and all everything looks fine but still am getting a 404 error, below is my code

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");
    }
}];

Upvotes: 0

Views: 2324

Answers (2)

Sam Berlin
Sam Berlin

Reputation: 3773

Based on your followup comments, after fixing the issues others pointed out you're now failing with insufficient scopes. In order to tell the server the user granted your app permission, you need to request OAuth authorization on behalf of the user. The iOS QuickStart explains how to do this, by creating an auth controller:

// 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; }

(The quickstart uses the spreadsheets.readonly scope, but I changed it to spreadsheets in the above because it looks like you want to modify data.)

You'll need to make sure the createAuthController method is called correctly (and only when necessary), and that things flow properly when it finishes authorizing too. The quickstart guide explains that in detail.

Upvotes: 1

Hieu Dinh
Hieu Dinh

Reputation: 722

Delete /edit in you spreadsheetId

Add a number after E column in your range

Upvotes: 1

Related Questions