0
votes

I’m trying to get messages list from gmail on iOS. I successfully received access token (using OAuth 2.0) to my account. But the next i have to do is get messages list using exactly AFNetworking framework (that’s the aim). Tried to do everything i could just like here: Google API Users.messages: list but i got an error :

Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo=0x8e5f9b0 {com.alamofire.serialization.response.error.response= { URL: .../gmail/v1/users/me/messages } { status code: 403, headers { "Alternate-Protocol" = "443:quic,p=1"; "Cache-Control" = "private, max-age=0"; "Content-Encoding" = gzip; "Content-Type" = "application/json; charset=UTF-8"; Date = "Tue, 28 Jul 2015 10:54:07 GMT"; Expires = "Tue, 28 Jul 2015 10:54:07 GMT"; Server = GSE; "Transfer-Encoding" = Identity; Vary = "Origin, X-Origin";...

Here is my code:

#import "RSServerManager.h"
#import "AFNetworking.h"
#import "RSUser.h"
#import "RSAccessToken.h"

@interface RSServerManager()
@property (strong, nonatomic) AFHTTPRequestOperationManager *requestOperationManager;
@end

@implementation RSServerManager

+ (RSServerManager *) sharedManager {
    static RSServerManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[RSServerManager alloc] init];
    });
    return manager;  
}

- (id)init
{
    self = [super init];
    if (self) {
        NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/"];
        self.requestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    }
    return self;
}

GET method:

- (void) getMessagesList:(RSAccessToken *)accessToken
               onSuccess:(void (^)(__autoreleasing id *))success
               onFailure:(void (^)(NSError *, NSInteger))failure {

    NSDictionary *Parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                                                @"messages", @"fields",
                                                                    @"true", @"includeSpamTrash",
                                                @"https://mail.google.com/", @"scope",
                                                                   @"query", @"q",nil];

    AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *authValue = [NSString stringWithFormat:@"Bearer %@", accessToken.access_token];
   [requestSerializer setValue: authValue forHTTPHeaderField:@"Authorization"];
   [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    self.requestOperationManager.requestSerializer = requestSerializer;
    [self.requestOperationManager
     GET:@"gmail/v1/users/me/messages"
     parameters: Parameters
     success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
         NSLog(@"JSON : %@", responseObject);

         if (success) {
             success(nil);
         }
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
         if (failure) {
             failure(error, operation.response.statusCode);
         }
     }];
}

Please help me to find the correct way to get these message list using AFNetworking!!!

1
Did you enable Gmail-API in your developers console? Did you have one of the scopes mentioned in this link developers.google.com/gmail/api/v1/reference/users/messages/… in your code? - SGC
Thanx alot!!! After checking out the scopes, i found that the scope of my access token was not the same that i have in Users.messages: list!!( So i used single one scope(@"https://mail.google.com/", @"scope") the same for both methods. And the error dissapeared. P.S. According to my code, to make it work correct u need to delete parameters: "q" and "fields". - Roma Chopovenko
Np :) glad that your issue is resolved!! - SGC

1 Answers

0
votes

Enable Gmail-API in your developers console. Include one of the scopes mentioned in this link in your code.