“文档” 菜单
文档首页
/ /
Atlas Device SDKs
/ /

管理用户 API 密钥 - Swift SDK

在此页面上

  • 创建用户 API 密钥。
  • 查找用户 API 密钥
  • 启用或禁用用户 API 密钥
  • 删除用户 API 密钥

您可以使用用户的API 密钥身份验证实例(可通过用户的apiKeysAuth属性访问该实例)来管理用户 API 密钥。

您可以使用 API 密钥身份验证实例的createAPIKey方法创建用户 API 密钥。

警告

存储 API 密钥值

SDK 仅在您创建用户 API 密钥时返回该密钥的值。 请确保安全存储 key值,以便使用它登录。

如果丢失或未存储key值,则无法恢复。 您需要创建新的用户 API 密钥。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeysAuth];
// Create the API key
[client createAPIKeyWithName:@"someKeyName" completion:^(RLMUserAPIKey *apiKey, NSError *error) {
if (error != nil) {
// ... handle Error ...
} else {
// ... use apiKey ...
}
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
// User must not be an anonymous user.
let user = app.currentUser!
let client = user.apiKeysAuth
client.createAPIKey(named: "someKeyName") { (apiKey, error) in
guard error == nil else {
print("Failed to create key: \(error!)")
return
}
// Use apiKey
}

您可以使用 API 密钥身份验证实例的fetchAPIKey方法查找用户 API 密钥。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeysAuth];
// Fetch API key by a specific ObjectId
NSError *error = nil;
RLMObjectId *objectId = [[RLMObjectId alloc] initWithString:@"someObjectId" error:&error];
[client fetchAPIKey:objectId completion:^(RLMUserAPIKey *apiKey, NSError *error) {
if (error != nil) {
// ... handle error ...
} else {
// ... use apiKey ...
}
}];
// Fetch all API keys
[client fetchAPIKeysWithCompletion:^(NSArray<RLMUserAPIKey *> *keys, NSError *error) {
if (error != nil) {
// ... handle error ...
} else {
for (RLMUserAPIKey *key in keys) {
// ... use key ...
}
}
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
// Fetch a specific API key by ObjectId
client.fetchAPIKey(ObjectId("00112233445566778899aabb")) { (maybeApiKey, error) in
// ...
}
// Fetch all API keys
client.fetchAPIKeys { (keys, error) in
guard error == nil else {
fatalError("Failed to fetch keys: \(error!)")
}
for key in keys! {
// use key
print(key.name)
}
}

您可以使用 API 密钥身份验证实例的enableAPIKeydisableAPIKey方法启用或禁用用户 API 密钥。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeysAuth];
// Enable the API key with specific objectId
RLMObjectId *objectId = [[RLMObjectId alloc] initWithString:@"00112233445566778899aabb" error:nil];
[client enableAPIKey:objectId completion:^(NSError *error) {
// Handle error if any. Otherwise, enable was successful.
}];
RLMUserAPIKey *apiKey;
// ... Get an API key ...
// Disable the API key
[client disableAPIKey:[apiKey objectId] completion:^(NSError *error) {
// Handle error if any. Otherwise, disable was successful.
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
// Enable the API key
client.enableAPIKey(ObjectId("00112233445566778899aabb")) { (error) in
// ...
}
let apiKey: UserAPIKey?
// ... Obtain a user API key ...
// Disable the API key
client.disableAPIKey(apiKey!.objectId) { (error) in
// ...
}

您可以使用 API 密钥身份验证实例的deleteAPIKey方法删除用户 API 密钥。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeysAuth];
RLMUserAPIKey *apiKey;
// ... Get an API key ...
[client deleteAPIKey:[apiKey objectId] completion:^(NSError *error) {
// Handle error if any. Otherwise, delete was successful.
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
let apiKey: UserAPIKey?
// ... Obtain a user API key ...
client.deleteAPIKey(apiKey!.objectId) { (error) in
guard error == nil else {
print("Failed to delete key: \(error!)")
return
}
// Key deleted
}

后退

关联用户身份

来年

同步数据