What am I doing wrong with the Pocket API for Objective-C that's causing archive commands to fail?

StackOverflow https://stackoverflow.com/questions/21071482

  •  27-09-2022
  •  | 
  •  

سؤال

I'm really scratching my head at this one. I'm using the Pocket API to allow users to archive Pocket articles from my app, but whenever I try to do so with the below code I get this error:

Error Domain=PocketSDK Code=400 "Invalid request, please refer to API documentation" UserInfo=0xc17d3b0 {NSLocalizedDescription=Invalid request, please refer to API documentation}

Code:

          NSDictionary *arguments = @{@"action": @"archive",
                                                 @"item_id": articleID};

          [[PocketAPI sharedAPI] callAPIMethod:@"send" withHTTPMethod:PocketAPIHTTPMethodPOST arguments:arguments handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error) {
                if (!error) {
                     NSLog(@"Archived article.");
                }
          }];

Exactly what part of that is incorrect? Am I not POSTing a send method to the API?

EDIT: I even changed it to have @"action" be @"actions" and to supply it the above NSDictionary, and it returns without an error but doesn't affect it on the Pocket website...

EDIT 2: Per the response of Joseph Chen I changed my code to the following:

      // Create data to pass to the Pocket API (a JSON array of actions)
      NSError *error;
      NSArray *actions = @[@{@"action": @"archive",
                                    @"item_id": articleID}];
      NSData *actionsAsJSONData = [NSJSONSerialization dataWithJSONObject:actions options:kNilOptions error:&error];
      NSString *actionsAsJSONString = [[NSString alloc] initWithData:actionsAsJSONData encoding:NSUTF8StringEncoding];

      NSDictionary *arguments = @{@"actions": actionsAsJSONString};

      [[PocketAPI sharedAPI] callAPIMethod:@"send" withHTTPMethod:PocketAPIHTTPMethodPOST arguments:arguments handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error) {
            if (!error) {
                 NSLog(@"%@", response);
            }
            else {
                 NSLog(@"%@", error);
            }
      }];

Which returns:

action_results" =     (
    1
);
status = 1;

Yet when I go to the website and log in, the article I "archived" is still staring me in the face, unarchived.

هل كانت مفيدة؟

المحلول 2

Here's the code taken (almost) straight from my app:

NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
NSDictionary *arguments = @{@"actions" : @[@{@"action" : @"archive",
                                             @"item_id" : itemId,
                                             @"time" : [NSString stringWithFormat:@"%ld", (long)timestamp]}]};

[self.pocketAPI callAPIMethod:@"send"
               withHTTPMethod:PocketAPIHTTPMethodPOST
                    arguments:arguments
                      handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error)
 {
     if (!error) {
         // OK
     } else {
         // handle error
     }
 }];

نصائح أخرى

According to the documentation the actions parameter should be a JSON dictionary. So you could either...

  1. Create the JSON dictionary manually:

    NSString *jsonString = [NSString stringWithFormat:@"[{\"action\":\"archive\",\"item_id\":\"%@\"}]", articleID]; // articleID is a NSString?
    NSDictionary *arguments = @{@"actions": jsonString};
    
  2. Use NSJSONSerialization:

    NSDictionary *actions = @{@"action": @"archive", @"item_id": articleID};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:actions
                                                       options:kNilOptions
                                                         error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData 
                                                 encoding:NSUTF8StringEncoding];
    NSDictionary *arguments = @{@"actions": jsonString};
    

This answer is also a reference.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top