Rでドメインを買うためにGoDaddy APIを使う

というタイトルですが、まだ買ってません。途中で挫折しました。

やったところまでのメモ。

準備

アカウント作成

アカウントをつくります。詳細は省略。APIを使うには、支払い方法はGood as Goldでないとだめらしいです。

APIキーを取得

APIを使うにはキーとシークレットが必要なのでそれを取得します。

f:id:yutannihilation:20170804213651p:plain:w450

f:id:yutannihilation:20170804213705p:plain:w450

Get Startedしてみる

とりあえず以下のドキュメントをなぞっていきます。

https://developer.godaddy.com/getstarted

「Making Calls」という例に従ってリクエストを送ってみます。認証には、Authorization: sso-key APIキー:APIシークレットというヘッダが必要です。

library(httr)

auth <- sprintf('sso-key %s:%s',
                 Sys.getenv("GODDADY_KEY"),
                 Sys.getenv("GODDADY_SECRET"))

res <- GET("https://api.godaddy.com/v1/domains/available?domain=example.guru",
           add_headers(`Authorization` =  auth))

res
#> Response [https://api.godaddy.com/v1/domains/available?domain=example.guru]
#>   Date: 2017-08-04 08:47
#>   Status: 401
#>   Content-Type: application/json
#>   Size: 98 B

なんかエラーになります。

実ははじめてつくったAPIキー&シークレットはテスト用のやつらしく、/v1/domains/availableには使えないみたいでした。ということで、本番用のキーを取得します。矢印のやつです。

f:id:yutannihilation:20170804213725p:plain:w450

res
#> Response [https://api.godaddy.com/v1/domains/available?domain=example.guru]
#>   Date: 2017-08-04 08:48
#>   Status: 200
#>   Content-Type: application/json; charset=utf-8
#>   Size: 105 B

今度はうまくいきました。中身はこんな感じ。

content(res)
#> $available
#> [1] FALSE
#> 
#> $domain
#> [1] "example.guru"
#> 
#> $definitive
#> [1] TRUE
#> 
#> $price
#> [1] 9990000
#> 
#> $currency
#> [1] "USD"
#> 
#> $period
#> [1] 1
#> 

高い。

Domain Purchaseをやってみる

引き続き上のドキュメントやってみます。

TLDの一覧を取得

Step 1. List of Top Level Domains (TLDs)

こんな感じです。

res <- GET("https://api.godaddy.com/v1/domains/tlds",
           add_headers(`Authorization` =  auth))

tlds <- content(res)
dplyr::bind_rows(tlds)
#> # A tibble: 457 x 2
#>           name         type
#>          <chr>        <chr>
#>  1     academy      GENERIC
#>  2 accountants      GENERIC
#>  3       actor      GENERIC
#>  4       adult      GENERIC
#>  5          ag COUNTRY_CODE
#>  6      agency      GENERIC
#>  7    airforce      GENERIC
#>  8          am COUNTRY_CODE
#>  9   amsterdam      GENERIC
#> 10  apartments      GENERIC
#> # ... with 447 more rows

457…そんなにあるんですね。

ドメイン名が使えるか調べる

Step 2. Check Domain Availability

これはさっきやったからスキップ。

ドメイン名の提案をうける

Step 3. [Optional] Domain Suggestions

おもしろそうではあるけどスキップ。

TLDスキーマ

Step 4. [Optional] Schema for TLDs

これは、次に実際にドメインを購入するために必要なフォーマットを調べます。たとえばcomについて調べてみましょう。

res <- GET("https://api.godaddy.com/v1/domains/purchase/schema/com",
           add_headers(`Authorization` =  auth))

schema <- content(res)
names(schema)
#> [1] "id"                   "additionalProperties" "properties"          
#> [4] "required"             "$schema"              "definitions" 

いろいろあります。

よく分からないんですが、propertiesに指定できる項目が入っているっぽいです。

names(schema$properties)
#>  [1] "domain"            "consent"           "period"           
#>  [4] "renewAuto"         "privacy"           "nameServers"      
#>  [7] "contactAdmin"      "contactBilling"    "contactRegistrant"
#> [10] "contactTech"

requredに入っているのがそのうち必須な項目です。

purrr::flatten_chr(schema$required)
#> [1] "domain"            "consent"           "contactAdmin"     
#> [4] "contactBilling"    "contactRegistrant" "contactTech"

propertiesの中にはその項目のフォーマットが入っています。

schema$properties$domain
#> $type
#> [1] "string"
#> 
#> $format
#> [1] "domain"
#> 
#> $pattern
#> [1] "^[^.]{1,63}(\\.[^.]{2,}){1,2}$"
#> 

中にはこんな感じに直接定義が書かれていないものがあります。

schema$properties$consent
#> $`$ref`
#> [1] "https://domain.api.int.godaddy.com/DomainPurchase#/definitions/Consent"
#> 

このURLにアクセスしようとしても、そんなサーバはない!というエラーになるしよく分からないなあと思ってたら、この定義がdefinitionsの方に入っているみたいでした。

names(schema$definitions)
#> [1] "Consent" "Contact" "Address"

str(schema$definitions$Consent)
#> List of 4
#>  $ id                  : chr "Consent"
#>  $ additionalProperties: logi FALSE
#>  $ properties          :List of 3
#>   ..$ agreementKeys:List of 3
#>   .. ..$ type       : chr "array"
#>   .. ..$ items      :List of 1
#>   .. .. ..$ type: chr "string"
#>   .. ..$ description: chr "Unique identifiers of the legal agreements to which the end-user has agreed, as returned from the/domains/agreements endpoint"
#>   ..$ agreedBy     :List of 2
#>   .. ..$ type       : chr "string"
#>   .. ..$ description: chr "Originating client IP address of the end-user's computer when they consented to these legal agreements"
#>   ..$ agreedAt     :List of 3
#>   .. ..$ type       : chr "string"
#>   .. ..$ format     : chr "iso-datetime"
#>   .. ..$ description: chr "Timestamp indicating when the end-user consented to these legal agreements"
#>  $ required            :List of 3
#>   ..$ : chr "agreementKeys"
#>   ..$ : chr "agreedBy"
#>   ..$ : chr "agreedAt"

このスキーマを見ながら/v1/domains/purchaseにPOSTリクエストを送ると(あとアカウントに入金しとくと)ドメインが買えるっぽいんですが、項目が多いので心がくじけました。。

ちなみに

ちなみに、これを考えているうちに気づいたんですがたぶんこのままだと自分の住所がWhoisに晒されてしまうので、以下のサービスを使う必要があります。

1000円かー。これを考えると、値段的にはお名前.comとかの方がお得ですね。1ドメインだけなら別にいいかなという感じですけど。