您的位置:首页 > 移动开发 > IOS开发

Instagram_OAuth2.0_iOS登录认证 Framework

2014-04-29 18:07 155 查看
最近在做各个应用的第三方授权登录,Facebook 、instagram、google+、twitter、QQ、Sina等。

大部分都有现成的framework, 当然instagram也有documentation讲解。

但是和其他应用不同的是,其他应用授权成功后一般都有一个userid/openid/uid 和accesstoken 作为登录自己应用的参数,但是 按照instagram 的 documentation中client端的授权方式,只能拿到一个accesstoken--直接导致后面的获取用户信息的API无法使用。所以自己封装了一个framework,获取授权信息和用户信息。

传送门

github源码

(Twitter正在研究中,iOS自身已经支持从setting中读取twitter信息,但是读取的信息也不够多,所以正在尝试web授权方式)

framework运行环境:iOS7.0+ 、arc

接口类:

//
//  InstagramAuthorizeManager.h
//  ILSLoginManager
//
//  Created by hupeng on 14-4-29.
//  Copyright (c) 2014年 toprankapps. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "InstagramMacro.h"

@interface InstagramAuthorizeManager : NSObject

+ (InstagramAuthorizeManager *)sharedManager;

+ (void)authorizeWithClientID:(NSString *)clientID
clientSecrect:(NSString *)clientSecrect
redirectURI:(NSString *)redirectURI
parentViewController:(UIViewController *)viewController
completionHandler:(InstagramAuthorizeCompletionHandler)completionHandler;

@end


可获取的信息如下:

//
//  InstagramModel.h
//  ILSLoginManager
//
//  Created by hupeng on 14-4-29.
//  Copyright (c) 2014年 toprankapps. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface InstagramModel : NSObject

// 在返回信息中将被清空
@property (nonatomic, strong) NSString *clientID;
@property (nonatomic, strong) NSString *clientSecrect;
@property (nonatomic, strong) NSString *redirectURI;
@property (nonatomic, strong) NSString *code;
// 返回信息
@property (nonatomic, strong) NSString *accessToken;
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *bio;
@property (nonatomic, strong) NSString *website;
@property (nonatomic, strong) NSString *image;
@property (nonatomic, strong) NSString *fullName;
@property (nonatomic, strong) NSString *uid;

@end


Example(下面是我自己管理第三方授权的Manager中的使用情况):

- (void)openInstagramAuthorizePage
{
// 1.login using cached data
if ([ILSCPSocialManager instagramHasLogin]) {
[self loginWithInstagramAccount];
return;
}
ILSCPSocialManager *manager = [ILSCPSocialManager sharedManager];

[InstagramAuthorizeManager authorizeWithClientID:manager.instagramClientID
clientSecrect:manager.instagramClientSecrect
redirectURI:manager.instagramRedirectURL
parentViewController:_parentViewController
completionHandler:^(NSError *error, InstagramModel *instagramInstance) {

if (error) {
_completionHandler(error);
return;
}
// init userprofile
_profile.userName = instagramInstance.uid;
_profile.sex = nil;
_profile.firstName = nil;
_profile.lastName = nil;
_profile.email = nil;

// 3.save login informations
[ILSCPSocialManager sharedManager].instagramAccessToken = instagramInstance.accessToken;
[ILSCPSocialManager sharedManager].instagramUID = instagramInstance.uid;
// 4.login
_requestStartHandler();

[self loginWithInstagramAccount];
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: