AFNetworking 2.0 and authorizing the Imgur API

Imgur's API requires that for simply looking up information about an image you just need to authorize your app with your API keys, no need to log in with an account or anything.

It says:

...all you need to do is send an authorization header with your client_id in your requests

Which apparently looks like:

Authorization: Client-ID YOUR_CLIENT_ID

So I tried doing this using AFHTTPRequestOperationManager , which appears to be the replacement for AFHTTPClient in AFNetworking 2.0 as shown below:

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];

[operationManager POST:@"https://api.imgur.com/3/image/1Nf1quS" parameters:@{@"Authorization": @"Client-ID ---"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failure");
}];

Basically tried to send a request with authorization information (I removed my ID for this post). It keeps giving "failure" as a response however.

So I tried playing around with the credential property but NSURLCredential seems to be based off a username and password, and I have neither of those as I just need my client ID.

So I tried a completely different way again:

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.imgur.com/3/image/1Nf1quS"]];
[request addValue:@"Client-ID ---" forHTTPHeaderField:@"Authorization"];

[operationManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"fail");
}];

This time using the authorization things as a value on the request. But this one actually never even logged anything.

I'm quite new to API use, so I'm really confused what I'm doing wrong.


Edit:

Try this code snippet

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager.requestSerializer setValue:@"Client-ID ---" forHTTPHeaderField:@"Authorization"];

[operationManager GET:@"https://api.imgur.com/3/image/1Nf1quS" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failure");
}];

I don't know about Imgur API, but for handling header and stuff, if you are just gonna do a few request to the API, you may add the headers just before the request operation. However, if you need to make API calls in more than one place, I think subclassing AFHTTPRequestOperationManager would be a better way to handle this.

For example you can create a subclass named MRTImgurRequestOperationManager:

@interface MRTImgurRequestOperationManager : AFHTTPRequestOperationManager
    + (MRTImgurRequestOperationManager*)sharedManager;
@end

and then in your implementation file:

+ (MRTImgurRequestOperationManager*)sharedManager
{
    static MRTImgurRequestOperationManager *_sharedManager;
    static dispatch_once_t _dispatchOnceToken;
    dispatch_once(&_dispatchOnceToken, ^{
        NSURL *baseURL = [NSURL URLWithString:@"API_URL_HERE"];
        _sharedManager = [[MRTImgurRequestOperationManager alloc] initWithBaseURL:baseURL];
    });

    return _sharedManager;
}

- (id)initWithBaseURL:(NSURL*)url
{
    self = [super initWithBaseURL:url];
    if (self)
    {
        // Add headers or other options here
        // For example
        [self.requestSerializer setValue:@"VALUE" forHTTPHeaderField:@"HEADER_NAME"];
    }
    return self;
}

This way, you can add/remove HTTP headers without ever changing you code in couple of places. This may help with the testing as well.

So to use this, you would:

#import "MRTImgurRequestOperationManager.h"

and then make your Imgur request with the shared manager.

Edit:

You should use GET, not POST, with the API endpoint you are using

[[MRTImgurRequestOperationManager sharedManager] GET:@"path" parameters:params success:success failure:failure]];

In AFNetworking 2.0 you can set header fields. By using method from AFHTTPRequestSerializer

- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

Try some thing like this:

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager.requestSerializer setValue:@"Client-ID ---" forHTTPHeaderField:@"Authorization"];

[operationManager POST:@"https://api.imgur.com/3/image/1Nf1quS" parameters:@{@"Authorization": @"Client-ID ---"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failure");
}];
链接地址: http://www.djcxy.com/p/81438.html

上一篇: ImgurV3 API的哪些部分可以匿名使用?

下一篇: AFNetworking 2.0和授权Imgur API