0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IPアドレスが変動してもBrawl Stars APIからデータを取得する方法

Posted at

1.はじめに
私はIDNectというゲームのIDを共有できるアプリを制作・運営しています。アプリのユーザーをいかに増やすかと考えた際に、スマホゲームで人気のありAPIを公開しているブロスタのプロフィールカードを作成する機能があれば、ユーザーが増えるのではという考えに至りました(この機能は今後公開予定)。その際に、サンプルとしてGoogle Colabでコードを実行しようとしましたが、Google colabは毎回IPアドレスが変動するため、APIキーを登録したIPアドレスと一致しないためデータのエラーが発生していました。この問題を解消した方法を以下に書き記します。

2.実装
・Brawl Stars APIに登録 (https://developer.brawlstars.com/#/)
まずAPIに登録する必要があります。ここで躓くことはないと思うので、割愛させていただきます。

・ログインが完了するとキーの発行をします。右上の自分のユーザー名の部分をタップすると、My Accountが選択できると思います。
Screenshot 2025-02-14 at 7.00.58 PM.png

My Accountを選択し、右下にあるcreate new keyを表示してください Screenshot 2025-02-14 at 7.03.41 PM.png
そうするとこのようにキーを作成できる部分があると思います。Key NameとDescription(説明)の部分は適当に入力しておいて問題ありません。
Screenshot 2025-02-14 at 7.07.04 PM.png
Allowed IP Address←ここに45.79.218.79を入力してください
今回使用するのはRoyale APIが提供するProxyです。(Proxyというのはどういうものなのかはご自身で調べてください)
そしてapiを取得する際には本来のURL、https://api.brawlstars.com にアクセスする必要がありますが、今回は固定IPアドレスにするためhttps://bsproxy.royaleapi.dev を使用してください。
Royale APIについては以下の画像を参照してください。
Screenshot 2025-02-14 at 7.10.19 PM.png

Screenshot 2025-02-14 at 7.10.28 PM.png

そしてgoogle colabで以下のコードを実行すれば指定したユーザーのデータを取得できます

import requests

BASE_URL = "https://bsproxy.royaleapi.dev"
API_TOKEN = "あなたのAPIキー"

HEADERS = {
    "Authorization": f"Bearer {API_TOKEN}",
    "Accept": "application/json"
}

player_tag = "#PLAYER_TAG"

def get_player_data(player_tag):
    encoded_tag = player_tag.replace("#", "%23")
    url = f"{BASE_URL}/players/{encoded_tag}"
    response = requests.get(url, headers=HEADERS)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: {response.status_code}, {response.text}")
        return None

player_data = get_player_data(player_tag)

if player_data:
    print("プレイヤーデータを取得しました:")
    print(player_data)
else:
    print("プレイヤーデータの取得に失敗しました。")


3.おわりに
特に書くことはありませんが、固定IPアドレスがなくてもBrawl stars apiからデータを取得できます。ぜひデータ分析などでご利用ください。
自分のIPアドレスを知るには以下のコードを使用することで取得することもできます。

import requests

ip = requests.get('https://api.ipify.org').text
print(f'Current IP Address: {ip}')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?