문서 메뉴
문서 홈
/ /
Atlas Device SDK
/ /

사용자 API 키 관리 - Swift SDK

이 페이지의 내용

  • 사용자 API 키 만들기
  • 사용자 API 키 조회
  • 사용자 API 키 활성화 또는 비활성화
  • 사용자 API 키 삭제

사용자의 apiKeysAuth 속성을 통해 액세스할 수 있는 사용자의 API 키 인증 인스턴스 로 사용자 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
}

돌아가기

사용자 ID 연결

다음

데이터 동기화