Login with Third-Party Account

Last Updated on : 2023-04-17 08:32:09download

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 {
    [[ThingSmartUser sharedInstance] loginByFacebook:@"your_country_code" token:@"facebook_token" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

 func loginByFacebook() {
    ThingSmartUser.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 {
	[[ThingSmartUser 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() {
	ThingSmartUser.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 ThingSuccessHandler)success
                     failure:(nullable ThingFailureError)failure;

Parameters

Parameter Description
type The type of third-party login implemented by OAuth 2.0. For example, you can specify ap to implement login with an Apple account. Valid values:
  • tw: Twitter
  • ap: Apple
  • gg: Google
  • wx: WeChat
  • qq: Tencent QQ
  • "": an empty string to represent Tuya’s UID
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 {
	[[ThingSmartUser 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() {
	ThingSmartUser.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;

	[[ThingSmartUser 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
	ThingSmartUser.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.

	[[ThingSmartUser 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.

    ThingSmartUser.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.

	[[ThingSmartUser 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.

    ThingSmartUser.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 ThingSuccessHandler)success
              failure:(nullable ThingFailureError)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 {
        [[ThingSmartUser sharedInstance] loginByWechat:@"your_country_code" code:@"wechat_code" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginByWechat() {
    ThingSmartUser.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 ThingSuccessHandler)success
          failure:(nullable ThingFailureError)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 {
    [[ThingSmartUser 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() {
    ThingSmartUser.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)")
        }
    })
}