【问题标题】:GET gmail mail list with AFNetworking ios xcode使用 AFNetworking ios xcode 获取 gmail 邮件列表
【发布时间】:2015-10-19 00:15:52
【问题描述】:

我正在尝试从 iOS 上的 gmail 获取消息列表。我成功接收到我的帐户的访问令牌(使用 OAuth 2.0)。但接下来我要做的是使用 AFNetworking 框架获取消息列表(这就是目标)。试图做我想做的一切:Google API Users.messages: list 但我得到了一个错误:

Domain=com.alamofire.error.serialization.response Code=-1011 "请求失败:禁止 (403)" UserInfo=0x8e5f9b0 {com.alamofire.serialization.response.error.response= { URL: .../ gmail/v1/users/me/messages } { 状态码:403,标题 { “替代协议”=“443:quic,p=1”; “缓存控制”=“私人,最大年龄=0”; “内容编码”= gzip; “内容类型”=“应用程序/json;字符集=UTF-8”; 日期 =“2015 年 7 月 28 日星期二 10:54:07 GMT”; Expires = "2015 年 7 月 28 日星期二 10:54:07 GMT"; 服务器 = GSE; “传输编码” = 身份; Vary = "原点,X-原点";...

这是我的代码:

#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方法:

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

请帮助我找到使用 AFNetworking 获取这些消息列表的正确方法!!!

【问题讨论】:

  • 您是否在开发者控制台中启用了 Gmail-API?您的代码中是否有此链接developers.google.com/gmail/api/v1/reference/users/messages/… 中提到的范围之一?
  • 非常感谢!!!检查范围后,我发现我的访问令牌的范围与我在 Users.messages 中的范围不同:列表!!(所以我使用了一个范围(@"https://mail.google.com/", @"scope"),这两种方法都一样。而且错误消失。PS根据我的代码,要使其正常工作,您需要删除参数:“q”和“fields”。
  • Np :) 很高兴您的问题得到解决!

标签: ios email get afnetworking gmail-api


【解决方案1】:

在您的开发者控制台中启用 Gmail-API。在您的代码中包含此link 中提到的范围之一。

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 2014-11-24
    • 2016-12-12
    • 2020-09-04
    • 2016-06-03
    相关资源
    最近更新 更多