教印度小夥伴怎麼開發微信登入微信分享記錄
需要:
註冊微信開放平臺,建立應用並提交,稽核通過後,開啟微信開放平臺,點選管理應用,在稽核通過後的應用資訊頁面,申請微信登入
注意點:
1.建立應用的時候,iOS需要bundle id ,安卓需要package name, 建議開發人員提供給建立應用人員,需要保證建立的應用的bundleID和專案裡保持一致,不然呼叫微信登入授權會不成功
2.稽核通過後可以拿到appid 和secret,記錄下來給開發人員
3.申請開通微信登入需要開發者資質認證,開發者資質認證就是交錢,國內300元人民幣,國外120美元。
4.開發不用等上面這些,可以先做sdk接入,最後換掉appid ,secret就行了
-----------------------
開發:
開發其實網上有很多教程了,各種語言版本的。不過還是沒有想到作為國外不使用微信的開發來說,他們說下載的微信註冊不了。。手把手教怎麼接入SDK後,終於登入成功,印度小夥伴問了我一個問題,“微信資訊獲取到了,我們用微信的賬號和密碼來註冊我們的應用嗎”,這一瞬間我聯想到facebook的資訊盜取。。。我又用了很久時間和他們解釋我們不能拿到微信賬號的賬號和密碼,我們有別的方式來註冊我們的應用,巴拉巴拉。。。
簡易的開發流程:
1.Download the latest SDK,You can also add the [WechatOpenSDK] folder.download:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&token=&lang=zh_CN
2.target-link library
3.target - Info - URL Types:add appid
4.info.plist :add LSApplicationQueriesSchemes:wechat;weixin
5.wxapi registerapp (用appid,appdelegate.m)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //regist //5.wxapi registerapp [WXApi registerApp:APPID]; return YES; }
6.在點選開啟微信的方法中新增調起微信客戶端
if ([WXApi isWXAppInstalled]) {
//authorization,Call WeChat login
SendAuthReq *request = [[SendAuthReq alloc]init];
request.state = @"App";
request.scope = @"snsapi_userinfo";//Obtain public information
[WXApi sendReq:request];
NSLog(@"login-->%d",[WXApi sendReq:request]);
}else{
NSLog(@"wechat not install");
}
open wechat will call this method, add <WXApiDelegate>
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
//6.Handle the data passed when WeChat starts the App via URL.
[WXApi handleOpenURL:url delegate:self];
return YES;
}
7.wechat callback,開啟微信,微信會返回一個 code//7.WeChat callback, whether login or share success or not, is to follow this method @brief send a sendReq and receive WeChat response.
- (void)onResp:(BaseResp *)resp{
NSLog(@"resp:%d",resp.errCode);
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *rep = (SendAuthResp *)resp;
if (rep.errCode == 0) {
//success
[self wxlogin:rep.code];
}
}
}
8.把code,appid,secret拿去請求得到access_token,refresh_token,openid
[manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",URL_APPID,URL_SECRET,code] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //獲得access_token,然後根據access_token獲取使用者資訊請求。Get the user information request.
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dic %@",dic);
/*
access_token 介面呼叫憑證Interface call certificate
expires_in access_token介面呼叫憑證超時時間,單位(秒)
refresh_token 使用者重新整理access_token
openid 授權使用者唯一標識The unique identity of the authorized user.
scope 使用者授權的作用域,使用逗號(,)分隔User authorization scope.
unionid 當且僅當該移動應用已獲得該使用者的userinfo授權時,才會出現該欄位
*/
NSString* accessToken=[dic valueForKey:@"access_token"];
NSString* openID=[dic valueForKey:@"openid"];
他們問我這樣一個問題,微信登入後如何註冊,如果微信登入後賬號註冊,退出登入後再用微信登入,怎麼保證是同一個人。資料庫的儲存對映問題。token過期問題。
下面兩張圖輔助解釋邏輯流
9.使用上一步得到的access_token ,openid來請求獲取使用者資訊
[manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dic ==== %@",dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error %ld",(long)error.code);
}];
獲取到的使用者資訊有:city; country; privileage; language; headimgurl; unionid; nickname; sex
當然不會拿到微信的登入賬號和密碼。。
RN的做法: