# 테스트용 사용자 API 키 생성

**URL:** https://meta.discourse.org/t/generate-user-api-keys-for-testing/145744
**Category:** Integrations
**Tags:** how-to
**Created:** [3월 26, 2020, 9:31오후 UTC](https://meta.discourse.org/t/generate-user-api-keys-for-testing/145744 "2020-03-26T21:31:38Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Discourse](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/32/148734_2.png) [@Discourse](https://meta.discourse.org/u/Discourse)
#### Post date: [3월 26, 2020, 9:31오후 UTC](https://meta.discourse.org/t/generate-user-api-keys-for-testing/145744/1 "2020-03-26T21:31:38Z")

</div>

[User API keys specification](https://meta.discourse.org/t/user-api-keys-specification/48536)에서의 토론을 이어갑니다:

로컬에서 User API 키를 테스트하기 위해 작은 유틸리티 스크립트를 작성했습니다.

먼저 종속성을 설치합니다:

```plaintext
gem install addressable openssl base64 json

```

다음은 스크립트입니다:

```ruby
require 'addressable'
require 'openssl'
require 'base64'
require 'json'

PRIVATE_KEY = OpenSSL::PKey::RSA.new(2048)
PUBLIC_KEY = PRIVATE_KEY.public_key

puts 'What is the target site?'
site = STDIN.gets.chomp

template = Addressable::Template.new("#{site}/user-api-key/new{?query*}")

url = template.expand({
  query: {
    application_name: 'ruby',
    client_id: `hostname`,
    scopes: 'read',
    public_key: PUBLIC_KEY,
    nonce: 1
  }
})

puts "navigate to #{url}."
puts
puts "copy the generated key in here"
puts
puts "press ENTER type end and press ENTER again"
puts

$/ = "end"
encoded_key = STDIN.gets.chomp

user_api_key = JSON.parse(PRIVATE_KEY.private_decrypt(Base64.decode64(encoded_key)))

puts "Your User API Key is #{user_api_key['key']}"

```
