LoginSignup
5
2

DropboxAPIを使ってテナントの種類を判定する

Posted at

はじめに

Dropbox for Businessのテナントには現在「チームフォルダテント」と「チームスペーステナント」の2種類があります。今後チームスペーステナントが増えていく(チームフォルダテナントだったものを順次切り替えていく)そうですが、テナントの種類によってAPIの挙動が変わるので見に行っているのがどっちのテナントなのか、はっきりさせる必要があります。

判定する方法

/get_current_accountのapiの中身を見て判断する

/get_current_accountの中の['root_info']['.tag']の中身がteamかuserかで判定できます。
teamならチームスペーステナント、userならチームフォルダテナント(従来のテナント)となっているようです。

実装例(python)

以下はpythonで取得する例です

get_current_account.py
import requests

def get_current_account(access_token):
    url = "https://api.dropboxapi.com/2/users/get_current_account"
        headers = {
            "Authorization": f"Bearer {access_token}",
        }
    
    response = requests.post(url, headers=headers)
    
    if response.status_code == 200:
        return response.json()
    else:
        return {"error" : "情報の取得に失敗しました。認証情報などを確認してください"}

# 各自のの環境のアクセストークンを設定
access_token = "YOUR_ACCESS_TOKEN"
current_account_info = get_current_account(access_token)
print(account_info)

実行時の返り値

対象のアカウントがテナントに紐づいている場合、下記のような値が返ってきます。
※以下は公式ドキュメントのreturns

{
    "account_id": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc",
    "account_type": {
        ".tag": "business"
    },
    "country": "US",
    "disabled": false,
    "email": "franz@dropbox.com",
    "email_verified": true,
    "is_paired": true,
    "locale": "en",
    "name": {
        "abbreviated_name": "FF",
        "display_name": "Franz Ferdinand (Personal)",
        "familiar_name": "Franz",
        "given_name": "Franz",
        "surname": "Ferdinand"
    },
    "referral_link": "https://db.tt/ZITNuhtI",
    "root_info": {
        ".tag": "user",
        "home_namespace_id": "3235641",
        "root_namespace_id": "3235641"
    },
    "team": {
        "id": "dbtid:AAFdgehTzw7WlXhZJsbGCLePe8RvQGYDr-I",
        "name": "Acme, Inc.",
        "office_addin_policy": {
            ".tag": "disabled"
        },
        "sharing_policies": {
            "group_creation_policy": {
                ".tag": "admins_only"
            },
            "shared_folder_join_policy": {
                ".tag": "from_anyone"
            },
            "shared_folder_link_restriction_policy": {
                ".tag": "anyone"
            },
            "shared_folder_member_policy": {
                ".tag": "team"
            },
            "shared_link_create_policy": {
                ".tag": "team_only"
            }
        }
    },
    "team_member_id": "dbmid:AAHhy7WsR0x-u4ZCqiDl5Fz5zvuL3kmspwU"

結局見るのはどこか

必要なのは下記の部分です。

    "root_info": {
        ".tag": "user",
    },

上記の値を基に処理を分けます

get_current_account.py

#get_current_accountは省略

# ~~~~~~~~~~~~~~~~

# 各自のの環境のアクセストークンを設定
access_token = "YOUR_ACCESS_TOKEN"
current_account_info = get_current_account(access_token)
try:
    if current_account_info["root_info"][".tag"] == "team":
        # チームスペーステナント時の処理
        print("利用中のテナントはチームスペーステナントです")

    elif current_account_info["root_info"][".tag"] == "team":
        # チームフォルダテナント時の処理
        print("利用中のテナントはチームフォルダテナントです")

except Exception as e:
    if "error" in current_account_info: 
        print(current_account_info["error"])
    else:
        print("不明なエラーが発生しました。")



おわりに

 特に/team_folder系のAPIがテナントの種類によって影響があるようですが、今回は見分け方のみとさせていただきます。テナントの種類の見分け方について記載しました。

参考資料

5
2
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
5
2