如何从flickrkit调用Flickr.addComments方法或如何使用Oauth向Flickr Api进行身份验证调用

时间:2019-01-22 19:18:40

标签: ios swift xcode flickr devdefined-oauth

我正在尝试实现FKFlickrPhotosCommentsAddComment,这是Flickrkit的一部分。如何使用Swift代码实现它?我已经完成了登录页面的用户身份验证,可以成功进入Flickr。我找到了其他不需要“写”权限的api,但是对于addComments,它说您需要写权限。我知道API调用现在需要OAuth参数:调用者的消费者密钥,消费者秘密(采用SHA编码)和其他必需的参数。

我现在可以使用方法FKFlickrPhotosCommentsAddComment或使用所需的参数手动创建api链接。我没有得到如何在我的代码中调用FKFlickrPhotosCommentsAddComment的方法。如果您可以帮助我。如果没有,您能否在代码中显示如何在我的代码下面的给定屏幕快照中提取参数,因为这些参数是在身份验证和用户登录期间调用的。

我正在尝试学习并且一直在阅读整个FlickrKit,并且一直在“跳到定义”广告“称霸体系:但是我无法弄清楚。

这是FLickrkit内部方法的代码:

#import "FKFlickrPhotosetsCommentsAddComment.h" 

@implementation FKFlickrPhotosetsCommentsAddComment



- (BOOL) needsLogin {
    return YES;
}

- (BOOL) needsSigning {
    return YES;
}

- (FKPermission) requiredPerms {
    return 1;
}

- (NSString *) name {
    return @"flickr.photosets.comments.addComment";
}

- (BOOL) isValid:(NSError **)error {
    BOOL valid = YES;
    NSMutableString *errorDescription = [[NSMutableString alloc] initWithString:@"You are missing required params: "];
    if(!self.photoset_id) {
        valid = NO;
        [errorDescription appendString:@"'photoset_id', "];
    }
    if(!self.comment_text) {
        valid = NO;
        [errorDescription appendString:@"'comment_text', "];
    }

    if(error != NULL) {
        if(!valid) {    
            NSDictionary *userInfo = @{NSLocalizedDescriptionKey: errorDescription};
            *error = [NSError errorWithDomain:FKFlickrKitErrorDomain code:FKErrorInvalidArgs userInfo:userInfo];
        }
    }
    return valid;
}

- (NSDictionary *) args {
    NSMutableDictionary *args = [NSMutableDictionary dictionary];
    if(self.photoset_id) {
        [args setValue:self.photoset_id forKey:@"photoset_id"];
    }
    if(self.comment_text) {
        [args setValue:self.comment_text forKey:@"comment_text"];
    }

    return [args copy];
}

- (NSString *) descriptionForError:(NSInteger)error {
    switch(error) {
        case FKFlickrPhotosetsCommentsAddCommentError_PhotosetNotFound:
            return @"Photoset not found";
        case FKFlickrPhotosetsCommentsAddCommentError_BlankComment:
            return @"Blank comment";
        case FKFlickrPhotosetsCommentsAddCommentError_UserIsPostingCommentsTooFast:
            return @"User is posting comments too fast.";
        case FKFlickrPhotosetsCommentsAddCommentError_SSLIsRequired:
            return @"SSL is required";
        case FKFlickrPhotosetsCommentsAddCommentError_InvalidSignature:
            return @"Invalid signature";
        case FKFlickrPhotosetsCommentsAddCommentError_MissingSignature:
            return @"Missing signature";
        case FKFlickrPhotosetsCommentsAddCommentError_LoginFailedOrInvalidAuthToken:
            return @"Login failed / Invalid auth token";
        case FKFlickrPhotosetsCommentsAddCommentError_UserNotLoggedInOrInsufficientPermissions:
            return @"User not logged in / Insufficient permissions";
        case FKFlickrPhotosetsCommentsAddCommentError_InvalidAPIKey:
            return @"Invalid API Key";
        case FKFlickrPhotosetsCommentsAddCommentError_ServiceCurrentlyUnavailable:
            return @"Service currently unavailable";
        case FKFlickrPhotosetsCommentsAddCommentError_WriteOperationFailed:
            return @"Write operation failed";
        case FKFlickrPhotosetsCommentsAddCommentError_FormatXXXNotFound:
            return @"Format \"xxx\" not found";
        case FKFlickrPhotosetsCommentsAddCommentError_MethodXXXNotFound:
            return @"Method \"xxx\" not found";
        case FKFlickrPhotosetsCommentsAddCommentError_InvalidSOAPEnvelope:
            return @"Invalid SOAP envelope";
        case FKFlickrPhotosetsCommentsAddCommentError_InvalidXMLRPCMethodCall:
            return @"Invalid XML-RPC Method Call";
        case FKFlickrPhotosetsCommentsAddCommentError_BadURLFound:
            return @"Bad URL found";

        default:
            return @"Unknown error code";
    }
}

@end

#import "FKFlickrAPIMethod.h"

typedef NS_ENUM(NSInteger, FKFlickrPhotosetsCommentsAddCommentError) {
    FKFlickrPhotosetsCommentsAddCommentError_PhotosetNotFound = 1,       /*  */
    FKFlickrPhotosetsCommentsAddCommentError_BlankComment = 8,       /*  */
    FKFlickrPhotosetsCommentsAddCommentError_UserIsPostingCommentsTooFast = 9,       /* The user has reached the limit for number of comments posted during a specific time period. Wait a bit and try again. */
    FKFlickrPhotosetsCommentsAddCommentError_SSLIsRequired = 95,         /* SSL is required to access the Flickr API. */
    FKFlickrPhotosetsCommentsAddCommentError_InvalidSignature = 96,      /* The passed signature was invalid. */
    FKFlickrPhotosetsCommentsAddCommentError_MissingSignature = 97,      /* The call required signing but no signature was sent. */
    FKFlickrPhotosetsCommentsAddCommentError_LoginFailedOrInvalidAuthToken = 98,         /* The login details or auth token passed were invalid. */
    FKFlickrPhotosetsCommentsAddCommentError_UserNotLoggedInOrInsufficientPermissions = 99,      /* The method requires user authentication but the user was not logged in, or the authenticated method call did not have the required permissions. */
    FKFlickrPhotosetsCommentsAddCommentError_InvalidAPIKey = 100,        /* The API key passed was not valid or has expired. */
    FKFlickrPhotosetsCommentsAddCommentError_ServiceCurrentlyUnavailable = 105,      /* The requested service is temporarily unavailable. */
    FKFlickrPhotosetsCommentsAddCommentError_WriteOperationFailed = 106,         /* The requested operation failed due to a temporary issue. */
    FKFlickrPhotosetsCommentsAddCommentError_FormatXXXNotFound = 111,        /* The requested response format was not found. */
    FKFlickrPhotosetsCommentsAddCommentError_MethodXXXNotFound = 112,        /* The requested method was not found. */
    FKFlickrPhotosetsCommentsAddCommentError_InvalidSOAPEnvelope = 114,      /* The SOAP envelope send in the request could not be parsed. */
    FKFlickrPhotosetsCommentsAddCommentError_InvalidXMLRPCMethodCall = 115,      /* The XML-RPC request document could not be parsed. */
    FKFlickrPhotosetsCommentsAddCommentError_BadURLFound = 116,      /* One or more arguments contained a URL that has been used for abuse on Flickr. */

};

/*

Add a comment to a photoset.


Response:

<comment id="97777-12492-72057594037942601" />

*/
@interface FKFlickrPhotosetsCommentsAddComment : NSObject <FKFlickrAPIMethod>

/* The id of the photoset to add a comment to. */
@property (nonatomic, copy) NSString *photoset_id; /* (Required) */

/* Text of the comment */
@property (nonatomic, copy) NSString *comment_text; /* (Required) */


@end


If not, this is another method for authentication, I would like to get the encoded signature in newArgs to call the addComment Flickr API which is an authenticated call:

@implementation FKURLBuilder

#pragma mark - URL Encryption

- (NSURL *) oauthURLFromBaseURL:(NSURL *)inURL method:(FKHttpMethod)method params:(NSDictionary *)params {

    NSDictionary *newArgs = [self signedOAuthHTTPQueryParameters:params baseURL:inURL method:method];
    NSMutableArray *queryArray = [NSMutableArray array];

    for (NSString *key in newArgs) {
        [queryArray addObject:[NSString stringWithFormat:@"%@=%@", key, FKEscapedURLStringPlus(newArgs[key])]];
    }

    NSString *newURLStringWithQuery = [NSString stringWithFormat:@"%@?%@", inURL.absoluteString, [queryArray componentsJoinedByString:@"&"]];

    return [NSURL URLWithString:newURLStringWithQuery];
}

//private
- (NSDictionary *) signedOAuthHTTPQueryParameters:(NSDictionary *)params baseURL:(NSURL *)inURL method:(FKHttpMethod)method {

    NSString *httpMethod = nil;
    switch (method) {
        case FKHttpMethodGET:
            httpMethod = @"GET";
            break;
        case FKHttpMethodPOST:
            httpMethod = @"POST";
            break;
        default:
            break;
    }

    NSMutableDictionary *newArgs = params ? [params mutableCopy] : [NSMutableDictionary dictionary];
    newArgs[@"oauth_nonce"] = [FKGenerateUUID() substringToIndex:8];
    NSTimeInterval time = [NSDate date].timeIntervalSince1970;
    newArgs[@"oauth_timestamp"] = [NSString stringWithFormat:@"%f", time];
    newArgs[@"oauth_version"] = @"1.0";
    newArgs[@"oauth_signature_method"] = @"HMAC-SHA1";
    newArgs[@"oauth_consumer_key"] = [FlickrKit sharedFlickrKit].apiKey;

    if (!params[@"oauth_token"] && [FlickrKit sharedFlickrKit].authToken) {
        newArgs[@"oauth_token"] = [FlickrKit sharedFlickrKit].authToken;
    }

    NSString *signatureKey = nil;
    if ([FlickrKit sharedFlickrKit].authSecret) {
        signatureKey = [NSString stringWithFormat:@"%@&%@", [FlickrKit sharedFlickrKit].secret, [FlickrKit sharedFlickrKit].authSecret];
    } else {
        signatureKey = [NSString stringWithFormat:@"%@&", [FlickrKit sharedFlickrKit].secret];
    }

    NSMutableString *baseString = [NSMutableString string];
    [baseString appendString:httpMethod];
    [baseString appendString:@"&"];
    [baseString appendString:FKEscapedURLStringPlus(inURL.absoluteString)];

    NSArray *sortedKeys = [newArgs.allKeys sortedArrayUsingSelector:@selector(compare:)];
    [baseString appendString:@"&"];

    NSMutableArray *baseStrArgs = [NSMutableArray array];
    for (NSString *key in sortedKeys) {
        [baseStrArgs addObject:[NSString stringWithFormat:@"%@=%@", key, FKEscapedURLStringPlus(newArgs[key])]];
    }   

    [baseString appendString:FKEscapedURLStringPlus([baseStrArgs componentsJoinedByString:@"&"])];

    NSString *signature = FKOFHMACSha1Base64(signatureKey, baseString);

    newArgs[@"oauth_signature"] = signature;
    printf("Its getting printed");
    NSLog(@"%@", signature);
    return newArgs;
}

0 个答案:

没有答案