1 阅读前提

        本开发攻略基于使用Authorization Code获取Access Token的授权验证流程,适用于android平台的应用接入。

OAuth2.0的授权验证示意图如下所示:

对于应用而言,需要进行两步:

1) 获取Authorization Code;

2) 通过Authorization Code获取Access Token

如果想了解OAuth2.0协议详细内容,可访问The OAuth 2.0 Authorization Protocol

2 接入准备

1) 请确保您准备接入的Spark平台服务已经启用,并核实各服务端口是否可以正常访问,默认Oauth服务端口为443

2) 请确保您移动设备已经安装好新版本Spark一网通客户端,扫码添加过目标平台,并能正常访问

3) 请确认您的应用已经在Spark平台创建,并成功获取app_ID和app_KEY

3 引用SDK,修改配置文件相关内容

引用spark-app-sdk-ios.framework到工程中

添加白名单,在info.plist 文件中添加先安一网通的白名单,可以参考 demo的plist文件配置,在 demo里把跳转的软件名和白名单保持一致

修改spark-app-sdk-ios.framework 内的SparkConfig.h宏定义

#pragma mark 宏定义
#define APPID               @"0667cc2816f747c6aaaa0efcfdd86667"
#define APPKEY              @"dQdfherR"
#define SCOPE               @"user"
#define REDIRECTURI         @"https://spark.syan.com.cn:443/oauth2/default.html"
#define ACCESSTOKENURL      @"https://spark.syan.com.cn/oauth2/oauth/token"
#define USEREXTENSION       @"https://spark.syan.com.cn/openapi/user/get_user_extension"
#define USERINFOURL         @"https://spark.syan.com.cn/openapi/user/get_user_info"

#define APPNAME             @"TestLaunchOtherApp"
#define PACKAGENAME         @"cn.com.syan.spark.client"

4 使用Spark一网通登录

应用调用一网通客户端的登录接口实现登录功能

调用一网通登录接口部分源代码如下:

NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
            
            
NSDictionary *dic =@{@"app_ID": APPID,
                    @"app_KEY": APPKEY,
                    @"scope": SCOPE,
                    @"redirect_URI": REDIRECTURI,
                    @"access_token_URL": ACCESSTOKENURL};
            
[[SparkConfig shareManager]loginToGetToken:dic AndIsFirst:YES];
            
            
[TokenGetService getAuthorizationCode:bundleId];


从获取的token中解析openid与应用系统帐号绑定关联关系。

5 PKCS1签名验证

PKCS1签名


self.type=@"signature";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
            
NSString *data = self.textFiled.text;
data = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, self.type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }

                    

PKCS1验证


NSString *type = @"verify";
NSString *signtype = @"p1";
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&type=%@&signtype=%@&sign=%@&data=%@&cert=%@&appId=%@&appName=%@&appLogo=%@", bundleId, type, signtype, sign, data, cert,appId,appName,logoString];
            
NSLog(@"%@", urlStr);
        NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
                    

6 PKCS7签名验证

PKCS7签名调用

self.type=@"signatureP7";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
            
NSString *data = self.textFiled.text;
data = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, self.type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
                    

PKCS7验签调用

NSString *type = @"verify";
NSString *signtype = @"p7";
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&type=%@&signtype=%@&sign=%@&data=%@&cert=%@&appId=%@&appName=%@&appLogo=%@", bundleId, type, signtype, sign, data, cert,appId,appName,logoString];
            
NSLog(@"%@", urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
    }
                    

7 加密解密

加密接口调用

NSString *type = @"encrypt";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
data = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
    }
                    

解密调用

NSString *type = @"decrypt";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
NSString *data = self.textView1.text;
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
    }
                    

8 数字信封

数字信封

NSString *type = @"DigitalEnvelope";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
data = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
    }
                    

拆封

NSString *type = @"UnDigitalEnvelope";
NSString *bundleId = [(AppDelegate *) [UIApplication sharedApplication].delegate bundleIdentifier];
NSString *appName=APPNAME;
NSString *appId = APPID;
            
UIImage *logo = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
NSData *logoData = UIImagePNGRepresentation(logo);
NSString *logoString = [logoData base64Encoding];
            
NSString *data = self.textView1.text;
            
NSString *urlStr = [NSString stringWithFormat:@"spark-client-ios://?bundleId=%@&appId=%@&type=%@&data=%@&appLogo=%@&appName=%@", bundleId, appId, type, data,logoString,appName];
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
    }

                    

9 SDK及Demo下载

IOS 接入Demo 下载