English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Login with Third-Party Account

Last Updated on : 2022-05-30 03:43:44download

To enable login with an account of a third-party platform, you must configure App ID and App Secret in the Third-Party Login Support section on the Tuya IoT Development Platform.

Then, you must develop the app as required by the third party, get the login code, and then call the login API method provided by Tuya Smart Life App SDK.

Login with Facebook account

API description

- (void)loginByFacebook:(NSString *)countryCode
                  token:(NSString *)token
                success:(nullable TYSuccessHandler)success
                failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code. Example: 86.
token The token granted by Facebook to enable login.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

ObjC:

- (void)loginByFacebook {
    [[TuyaSmartUser sharedInstance] loginByFacebook:@"your_country_code" token:@"facebook_token" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

 func loginByFacebook() {
    TuyaSmartUser.sharedInstance()?.login(byFacebook: "your_country_code", token: "facebook_token", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

Login with Twitter account

API description

- (void)loginByTwitter:(NSString *)countryCode
				key:(NSString *)key
				secret:(NSString *)secret
			success:(nullable TYSuccessHandler)success
			failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code. Example: 86.
key The key granted by Twitter to enable login.
secret The secret granted by Twitter to enable login.
success The success callback.
failure The failure callback.

Example

ObjC:


- (void)loginByTwitter {
	[[TuyaSmartUser sharedInstance] loginByTwitter:@"your_country_code" key:@"twitter_key" secret:@"twitter_secret" success:^{
		NSLog(@"login success");
	} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
	}];

}

Swift:

func loginByTwitter() {
	TuyaSmartUser.sharedInstance()?.login(byTwitter: "your_country_code", key: "twitter_key", secret: "twitter_secret", success: {
		print("login success")
	}, failure: { (error) in
		if let e = error {
			print("login failure: \(e)")
		}
	})
}

Login with OAuth 2.0

API description

The OAuth 2.0 API method is a common method for login. The type of OAuth 2.0 login is indicated by a parameter passed in.

- (void)loginByAuth2WithType:(NSString *)type
                 countryCode:(NSString *)countryCode
                 accessToken:(NSString *)accessToken
                   extraInfo:(NSDictionary *)extraInfo
                     success:(nullable TYSuccessHandler)success
                     failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
type The type of OAuth 2.0 login. For example, ap indicates login with an Apple account and gg indicates login with a Google account.
countryCode The country code. Example: 86.
accessToken The token granted for login.
extraInfo The additional information.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

ObjC:

- (void)loginWithAuth2 {
	[[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"auth2_type" countryCode:@"your_country_code" accessToken:@"auth2_token" extraInfo:@{@"info_key": @"info_value"} success:^{
		NSLog(@"login success");
	} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
	}];
}

Swift:

func loginWithAuth2() {
	TuyaSmartUser.sharedInstance().loginByAuth2WithType("auth2_type", countryCode: "your_country_code", accessToken: "auth2_token", extraInfo: ["info_key":"info_value"], success: {
		print("login success")
	}, failure: { (error) in
		if let e = error {
				print("login failure: \(e)")
		}
	})
}

Login with Apple account

API description

Smart Life App SDK v3.14.0 and later support login with an Apple account. After successful authorization, the OAuth 2.0 API method returns parameters such as token and extraInfo to enable login.

Parameters

Parameter Description
type @“ap”
countryCode The country code. Example: 86.
accessToken [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding]
extraInfo @{@“userIdentifier”: credential.user, @“email”: credential.email, @“nickname”:credential.fullName.nickname, @“snsNickname”: credential.fullName.nickname}
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

ObjC:

- (void)loginWithApple {
	ASAuthorizationAppleIDCredential *credential = authorization.credential;

	[[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"ap" countryCode:@"your_country_code" accessToken:credential.identityToken extraInfo:{@"userIdentifier": credential.user, @"email": credential.email, @"nickname": credential.fullName.nickname, @"snsNickname": credential.fullName.nickname} success:^{
		NSLog(@"login success");
	} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
	}];
}

Swift:

func loginWithApple() {
	let credential = authorization.credential
	TuyaSmartUser.sharedInstance().loginByAuth2(withType: "ap", countryCode: "your_country_code", accessToken: credential?.identityToken, extraInfo: 	["userIdentifier": user,"email": email,"nickname": nickname,"snsNickname": nickname], success: {
		print("login success")
	}, failure: { (error) in
		if let e = error {
				print("login failure: \(e)")
		}
	})
}

Login with Google account

API description

Smart Life App SDK v3.19.0 and later support login with a Google account. After successful authorization, the OAuth 2.0 API method returns parameters such as token or idToken granted by Google and extraInfo to enable login.

In view of the deployment of Google’s global network services, we recommend that users registered in mainland China do not log in with a Google account.

Parameters

Parameter Description
type @“gg”
countryCode The country code. Example: 86.
accessToken The idToken granted by Google.
extraInfo @{@“pubVersion”: @1}
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

- (void)loginWithGoogle {

    NSString *loginType = @"gg";    // Login with a Google account.
    NSString *countryCode = @"1";   // The USA.
    NSString *accessToken = @"id token from Google";  // The `id token` granted by Google.

	[[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"gg"
                                             countryCode:countryCode
                                             accessToken:accessToken
                                                extraInfo:@{@"pubVersion": @1}
    success:^{
		NSLog(@"login success");
	} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
	}];
}

Swift:

func loginWithGoogle() {

    let loginType = "gg" // Login in with a Google account.
    let countryCode = "1" // The USA.
    let accessToken = "id token from Google" // The `id token` granted by Google.

    TuyaSmartUser.sharedInstance().loginByAuth2(
        withType: "gg",
        countryCode: countryCode,
        accessToken: accessToken,
        extraInfo: [
            "pubVersion": NSNumber(value: 1)
        ],
        success: {
            print("login success")
        },
        failure: { error in
            if let error = error {
                print("login failure: \(error)")
            }
        })
}

Login with LINE account

API description

Smart Life App SDK v3.25.0 and later support login with a LINE account. After successful authorization, the OAuth 2.0 API method returns parameters such as token or idToken granted by LINE and extraInfo to enable login.

In view of the deployment of LINE’s global network services, we recommend that users registered in mainland China do not log in with a LINE account.

Parameters

Parameter Description
type @“line”
countryCode The country code. Example: 86.
accessToken The authentication id token granted by LINE.
extraInfo @{} nil returned.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

ObjC:


- (void)loginWithLine {

    NSString *loginType = @"line";    // Login with a LINE account.
    NSString *countryCode = @"1";   // The USA.
    NSString *accessToken = @"id token from line";  // The `id token` granted by LINE.

	[[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"line"
                                             countryCode:countryCode
                                             accessToken:accessToken
                                                extraInfo:@{}
    success:^{
		NSLog(@"login success");
	} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
	}];
}

Swift:

func loginWithLine() {

    let loginType = "line" // Login with a LINE account.
    let countryCode = "1" // The USA.
    let accessToken = "id token from line" // The `id token` granted by LINE.

    TuyaSmartUser.sharedInstance().loginByAuth2(
        withType: "line",
        countryCode: countryCode,
        accessToken: accessToken,
        extraInfo: [:],
        success: {
            print("login success")
        },
        failure: { error in
            if let error = error {
                print("login failure: \(error)")
            }
        })
}

Login with WeChat account

API description

- (void)loginByWechat:(NSString *)countryCode
                 code:(NSString *)code
              success:(nullable TYSuccessHandler)success
              failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code: 86.
code The code granted by WeChat to enable login.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

For login to the app using WeChat accounts, we recommend that you allow only WeChat accounts registered with China mobile phone numbers (+86) to be used for login. For WeChat accounts registered with mobile phone numbers in other countries or regions, the users might access WeChat services managed inside mainland China when these users are located outside mainland China, which may pose compliance risks of cross-border data transfers. As the data controller, you shall be aware of and bear the compliance risks posed by potential cross-border data transfers.

Example

ObjC:

- (void)loginByWechat {
        [[TuyaSmartUser sharedInstance] loginByWechat:@"your_country_code" code:@"wechat_code" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginByWechat() {
    TuyaSmartUser.sharedInstance()?.login(byWechat: "your_country_code", code: "wechat_code", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

Login with Tencent QQ account

API description

- (void)loginByQQ:(NSString *)countryCode
           userId:(NSString *)userId
      accessToken:(NSString *)accessToken
          success:(nullable TYSuccessHandler)success
          failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code: 86.
userId The user ID.
accessToken The accessToken granted by Tencent QQ to enable login.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

For login to the app using Tencent QQ accounts, we recommend that you allow only Tencent QQ accounts registered with China mobile phone numbers (+86) to be used for login. For Tencent QQ accounts registered with mobile phone numbers in other countries or regions, the users might access Tencent QQ services managed inside mainland China when these users are located outside mainland China, which may pose compliance risks of cross-border data transfers. As the data controller, you shall be aware of and bear the compliance risks posed by potential cross-border data transfers.

Example

ObjC:

- (void)loginByQQ {
    [[TuyaSmartUser sharedInstance] loginByQQ:@"your_country_code" userId:@"qq_open_id" accessToken:@"access_token" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

 func loginByQQ() {
    TuyaSmartUser.sharedInstance()?.login(byQQ: "your_country_code", userId: "qq_open_id", accessToken: "access_token", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}