2
0

More than 1 year has passed since last update.

VRChat APIで特定のGroupに通知する方法【2段階認証対応】

Last updated at Posted at 2023-06-21

はじめに

VRChat APIを使用して外部から通知でも飛ばそうかと思います。
VRC+を契約してgroup作れるようになったので備忘録です。

嘘です本当はあっきーすんの地震情報通知グルをみて憧れて作ったdiscordの通知をVRChatに飛ばすbot作ったときの副産物です

参考

VRChat APIは正式に認められた機能じゃないので気をつけましょう

この記事見なくても↑のとおりにすればいい

やりたいこと

これです
image.png
image.png

認証

APIKEY + ユーザーネーム + パスワードauthcookieが発行されます

APIKEY取得

JlE5~...固定なので別にいらないんですけどね

↑にブラウザとかcurlとかでアクセスして出てきたclientApiKeyのやつです
image.png

authcookie取得

authcookieは

https://api.vrchat.cloud/api/1/auth/user

にGETでレスポンスについてるcookieの中にあります
ドキュメント

get_authcookie.py
import requests

UN = "ユーザ名"
PW = "パスワード"
APIKEY = "JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26" #さっきのやつ

r = requests.get("https://api.vrchat.cloud/api/1/auth/user",
                headers = {"User-Agent": UN},
                data = {"apiKey": APIKEY},
                auth = (UN,PW))

print(r.cookies["auth"])

authcookie_から始まるやつが出てくると思います。それです。

2段階認証

2段階認証とかweb版をログアウトした人はgoogle authenticatorでコード拾ったりとかメールとかからコードが送られてきたりすると思います。その状態だとauthcookieがまだ有効化されていないのでauthcookieを送られてきたコードと共に

2段階認証を有効にしているならこっち ↓ に

https://api.vrchat.cloud/api/1/auth/twofactorauth/totp/verify

2段階認証を有効にしていなくてメールにコードが届く人はこっち ↓ に

https://api.vrchat.cloud/api/1/auth/twofactorauth/emailotp/verify

POSTすることでauthcookieを有効化できます

ドキュメント (2段階)
ドキュメント (メール)

2fa.py
#2段階認証はこっち
import requests

f2acode = input("f2acode: ")
cookie = "authcookie_**********"

requests.post("https://api.vrchat.cloud/api/1/auth/twofactorauth/totp/verify",
               headers={"User-Agent": UN,
                        "Content-Type": "application/json"},
               cookies={"auth": cookie},
               json={"code": f2acode})
mailverify.py
#メール認証はこっち
import requests

f2acode = input("f2acode: ")
cookie = "authcookie_**********"

requests.post("https://api.vrchat.cloud/api/1/auth/twofactorauth/emailotp/verify",
               headers={"User-Agent": UN,
                        "Content-Type": "application/json"},
               cookies={"auth": cookie},
               json={"code": f2acode})

グループに通知

グループに通知するには自分がそのグループ内で通知を発信する権限(ロール)がある必要があります。

グループに通知を飛ばすには認証で得たauthcookieをタイトルや内容とともに

https://api.vrchat.cloud/api/1/groups/ """ グループID """ /announcement

にPOSTすることでできます。
ドキュメント

グループIDはwebで開いたときのURLのgrp_から始まるこれです
image.png

これで

group_announcement.py
import json

authcookie = "authcookie_**********"
groupId = "grp_**************"

title = "タイトルです"
text = "テキスト(内容)です"

requests.post(f"https://api.vrchat.cloud/api/1/groups/{groupId}/announcement",
                json.dumps({"title": title, "text": text, "sendNotification": True}),
                headers = {"User-Agent": UN,
                            "Content-Type": "application/json"},
                cookies={"auth": authcookie})

飛ぶとおもいます。
imageIdというすでにアカウントにアップロードされてる画像をくっつけるパラメーターもあります。

まとめたやつ

main.py
import json
import requests

groupId = "grp_*****************"
title = ""
text = ""

UN = ""
PW = ""

base = "https://api.vrchat.cloud/api/1/"
headers = {"User-Agent": UN}

def getAuthCookie(UN:str, PW:str):
    r = requests.get(f"{base}auth/user",
                     headers = {"User-Agent": UN},
                     data = {"apiKey": "JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26"},
                     auth = (UN,PW))
    print("getAuthCookie", r)
    return r.cookies["auth"]

def f2a(cookie:str):
    f2acode = input("f2acode: ")
    r = requests.post(f"{base}auth/twofactorauth/emailotp/verify",
                      headers=headers,
                      cookies={"auth": cookie},
                      json={"code": f2acode})
    print("f2a", r)

def annoucement(groupId:str, title:str, text:str, authCookie:str):
    def send():
        r = requests.post(f"{base}groups/{groupId}/announcement",
                          json.dumps({"title": title, "text": text, "sendNotification": True}),
                          headers = {"User-Agent": UN, "Content-Type": "application/json"},
                          cookies={"auth": authCookie})
        print("annoucement", r.text)
        return r

    if json.loads(send().text)["error"]["message"] == "\"Requires Two-Factor Authentication\"":
        f2a(authCookie)
        send()

annoucement(groupId=groupId,
            title=title,
            text=text,
            authCookie=getAuthCookie(UN=UN, PW=PW))
2
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
2
0