移動(dòng)端集成Instagram登錄授權(quán)目前是不支持SDK哮伟,需要通過web形式加載url來獲取授權(quán)信息
1、申請(qǐng)應(yīng)用
先去https://developers.facebook.com/apps/創(chuàng)建應(yīng)用炬太,過程比較簡(jiǎn)單略過魂务。設(shè)置完成后進(jìn)入應(yīng)用炎疆,在控制面板中往下翻會(huì)找到Instagram
點(diǎn)擊設(shè)置
可以看到應(yīng)用的所有信息,Instagram 應(yīng)用編號(hào)和Instagram 應(yīng)用密鑰是自動(dòng)生成的勾拉;Instagram Display 名稱自定義煮甥;有效 OAuth 跳轉(zhuǎn) URI需要能正常跳轉(zhuǎn)的網(wǎng)址并且是https開頭的,這個(gè)后面授權(quán)成功會(huì)回調(diào)跳轉(zhuǎn)藕赞;取消授權(quán)回調(diào)網(wǎng)址和數(shù)據(jù)刪除請(qǐng)求網(wǎng)址可以與上面一樣成肘。
往下翻,找到
添加一名Instagram測(cè)試人員(必須添加否則會(huì)授權(quán)失敻伞)
到這里Instagram準(zhǔn)備工作已經(jīng)做完了双霍。
2、iOS授權(quán)
主要思路就是:通過webView加載獲取code的url批销,攔截回調(diào)的有效 OAuth 跳轉(zhuǎn) URI
獲取里面的code值洒闸,通過code獲取user_id
和access_token
,在通過user_id
和access_token
獲取用戶名等信息均芽。
直接上代碼:
#import "WebLoginController.h"
@interface InstagramLoginController : WebLoginController
@property (nonatomic, copy) void (^getUserInfoCallback)(BOOL isSuccess, NSDictionary * _Nullable userInfo) ;
@end
#import "InstagramLoginController.h"
#define Instagram_client_id @""
#define Instagram_client_secret @""
#define Instagram_URI @"https://www.baidu.com/"
@interface InstagramLoginController ()
@end
@implementation InstagramLoginController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat: @"https://api.instagram.com/oauth/authorize?client_id=%@&redirect_uri=%@&scope=user_profile,user_media&response_type=code", Instagram_client_id, Instagram_URI];
[self loadUrl:urlString];
}
- (BOOL)judgeUrl:(NSString *)urlString {
if ([urlString hasPrefix:Instagram_URI]) {
NSString *replaceString = [NSString stringWithFormat:@"%@?code=", Instagram_URI];
urlString = [urlString stringByReplacingOccurrencesOfString:replaceString withString:@""];
NSString *code = [urlString stringByReplacingOccurrencesOfString:@"#_" withString:@""];
NSLog(@"%@", urlString);
[self getUserDateWidthCode:code];
return NO;
}
NSLog(@"%@", urlString);
return YES;
}
//處理請(qǐng)求用戶數(shù)據(jù)
- (void)getUserDateWidthCode:(NSString *)code {
NSDictionary *params = @{
@"client_id": Instagram_client_id,
@"client_secret": Instagram_client_secret,
@"grant_type": @"authorization_code",
@"redirect_uri": Instagram_URI,
@"code": code,
};
[PublicDialogManager showWaittingInView:self.view];
[HTTPMANAGER startPostUrl:@"https://api.instagram.com/oauth/access_token" param:params success:^(NSDictionary * _Nullable resultDict) {
NSLog(@"%@", resultDict);
NSString *user_id = [resultDict stringWithFilted:@"user_id"];
NSString *access_token = [resultDict stringWithFilted:@"access_token"];
if (user_id.length > 0 && access_token.length > 0) {
NSString *userInfoUrl = [NSString stringWithFormat:@"https://graph.instagram.com/%@?fields=id,username&access_token=%@", user_id, access_token];
[HTTPMANAGER startGetUrl:userInfoUrl param:nil success:^(NSDictionary * _Nullable resultDict) {
[PublicDialogManager hideWaittingInView:self.view];
NSLog(@"%@", resultDict);
if (self.getUserInfoCallback) {
self.getUserInfoCallback(YES, resultDict);
}
[self popBack];
} failure:^(NSError * _Nullable error) {
[PublicDialogManager hideWaittingInView:self.view];
if (self.getUserInfoCallback) {
self.getUserInfoCallback(NO, nil);
}
NSLog(@"%@", error);
[self popBack];
}];
}else {
[PublicDialogManager hideWaittingInView:self.view];
if (self.getUserInfoCallback) {
self.getUserInfoCallback(NO, nil);
}
[self popBack];
}
} failure:^(NSError * _Nullable error) {
[PublicDialogManager hideWaittingInView:self.view];
if (self.getUserInfoCallback) {
self.getUserInfoCallback(NO, nil);
}
[self popBack];
NSLog(@"%@", error);
}];
}
- (void)popBack {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
再上一下WebLoginController
部分代碼:
@interface WebLoginController : BaseViewController
@property (nonatomic, strong) WKWebView *webView ;
@property (nonatomic, strong) NSString *urlString ;
- (void)loadUrl:(NSString *)urlString ;
@end
#import "WebLoginController.h"
@interface WebLoginController ()<WKNavigationDelegate>
@end
@implementation WebLoginController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
WKWebView *webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:webConfiguration];
[self.view addSubview:webView];
webView.navigationDelegate = self;
webView.opaque = NO;
webView.backgroundColor = UIColorHex(#f6f6f6);
self.webView = webView;
[webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(navigationBgView.mas_bottom);
make.left.right.bottom.mas_equalTo(0);
}];
}
- (void)loadUrl:(NSString *)urlString {
NSString *urlStr = urlString;
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
//子類重寫
- (BOOL)judgeUrl:(NSString *)urlString {
return YES;
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// 獲取完整url并進(jìn)行UTF-8轉(zhuǎn)碼
NSString *strRequest = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
if ([self judgeUrl:strRequest]) {
// 允許跳轉(zhuǎn)
decisionHandler(WKNavigationActionPolicyAllow);
}else {
// 不允許跳轉(zhuǎn)
decisionHandler(WKNavigationActionPolicyCancel);
}
}
@end
最后成功獲取信息丘逸,做一下回調(diào)就好了。