# Alipay
This social login method provides GitHub Demo: https://github.com/authing/AuthingIOSDemo (opens new window)
Example Demo:
# Prerequisites
You need to prepare the following in total:
- Register an Authing developer account
- Apply for a registered Alipay mobile application
- Fill in the Alipay mobile application information in the Authing console
# Apply to register for Alipay mobile application
Go to Alipay Open Platform (opens new window) to register the Alipay mobile application, you need to follow the instructions of this page (opens new window) Generate a pair of RSA key pairs, keep the private key properly, and submit the public key to Alipay in exchange for the Alipay public key.
Please make sure that the "Get Member Information" function is turned on.
# Fill in the Alipay mobile application information in the Authing console
In the Authing console Connect Identity Source -> Social Login page, find "Alipay Mobile Application" in "Mobile Social Login":
Fill in your Alipay mobile application information here:
- AppID (application ID): You can get it in Management Center-My Application List (opens new window).
- AlipayPID (Partner Account PID): You can get it on the Account Center-Partner Management (opens new window) page.
- AppPrivateKey: The application private key generated in the previous step.
# Formal access
# Access AlipaySDK-iOS
Please follow the instructions of Alipay Client iOS Integration Process (opens new window). If you encounter problems, here is a Swift Demo App for developers’ reference: https: //github.com/authing/AuthingIOSDemo (opens new window).
# Access AlipaySDK-iOS
Please follow the instructions of Alipay Client iOS Integration Process (opens new window). If you encounter problems, here is a Swift Demo App for developers’ reference: https: //github.com/authing/AuthingIOSDemo (opens new window).
# Get authInfo
To start Alipay, you need to obtain and generate authInfo from the server first, and developers can call our interface:
swift code example:
import Alamofire
let GetAuthInfoUrl = "https://core.authing.cn/connection/social/alipay/<YOUR_USERPOOL_ID>/authinfo"
func loginByAlipay() {
// Get authInfo
AF.request(GetAuthInfoUrl).responseString {response in
let resp = convertToDictionary(text: response.value!)!
// Authing business status code, 200 means success
let code = resp["code"]! as! Int
let message = resp["message"]! as! String
if code == 200 {
let authInfo = resp["data"]! as! String
debugPrint("authInfo: \(authInfo)")
// Open Alipay
AlipaySDK().auth_V2(withInfo: authInfo, fromScheme: "authing-alipay", callback: {resp in
debugPrint(resp)
})
} else {
debugPrint("Message: ", message)
}
}
}
# Open Alipay login
Swift sample code:
// Open Alipay
AlipaySDK().auth_V2(withInfo: authInfo, fromScheme: "authing-alipay", callback: {resp in
debugPrint(resp)
})
The fromSchema here needs to be consistent with the URL Schema in the set URL Types: as shown in the figure below:
# Receive Alipay callback data
You can get Alipay callback data in the following methods (see this Demo (opens new window) for details):
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)
Swift sample code:
func parseUrlQueryString(string: String) -> [String:Any] {
let arr = string.components(separatedBy:"&")
var data = [String:Any]()
for row in arr {
let pairs = row.components(separatedBy:"=")
data[pairs[0]] = pairs[1]
}
return data
}
// schema callback
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
let url = URLContexts.first?.url
let schema = url!.scheme
switch schema {
case AlipayURLSchema:
AlipaySDK().processAuth_V2Result(url, standbyCallback: {back in
let response = back!
let resultStatus = response["resultStatus"] as! String
let memo = response["memo"]
let result = response["result"] as! String
let data = parseUrlQueryString(string: result)
let success = data["success"]
let result_code = data["result_code"] as! String
let auth_code = data["auth_code"] as! String
let user_id = data["user_id"] as! String
if resultStatus == "9000" && result_code == "200" {
// proceed to the next logic
}
})
default:
debugPrint("???")
}
}
# Exchange user information
The auth_code obtained in the previous step can be exchanged for user information through the following API:
Swift code example:
let url = "https://core.authing.cn/connection/social/alipay/\(UserPoolId)/callback?auth_code=\(auth_code)"
AF.request(url).responseString {response in
// Convert response.value into a string, the example is as follows:
// ["code": "200", "message": "Obtaining user information successfully", data: ""]
let resp = convertToDictionary(text: response.value!)!
let data = resp["data"] as! [String: Any]
debugPrint("Data: \(data)")
}
# Next Step
Congratulations, you have now accessed the Alipay mobile application login. After obtaining the user information, you can get the login credential token. You can carry this token in subsequent API requests, and then distinguish different users based on this token in the backend interface. For details, see [Verification token](../ ../advanced/verify-jwt-token.md#yan-zheng-authing-qian-fa-de-token).
We also provide React Native SDK, which allows you to quickly access Alipay login in React Native:
SDK for React Native