Persisting token/auth record cache for DeviceCodeCredentials

I have a Python program that submits pipelines to Azure ML. This code typically runs on headless Linux VMs, and authenticates with Azure using the DeviceCodeCredentials flow. I want to cache these credentials so that I can run this script many times and only have to re-authenticate every so often (e.g. every hour).

According to the documentation, TokenCachePersistenceOptions should solve this, however it seems to have no effect beyond writing a useless file. This is what my code looks like:

from azure.ai.ml import MLClient
from azure.identity import DeviceCodeCredential, TokenCachePersistenceOptions

from .constants import AML_SUBSCRIPTION_ID, AML_RESOURCE_GROUP, AML_WORKSPACE


cache_path = os.path.expanduser("~/.azure/msal_token_cache.json")

token_cache_options = TokenCachePersistenceOptions(name=cache_path, allow_unencrypted_storage=True)
credential = DeviceCodeCredential(token_cache_persistence_options=token_cache_options)

client = MLClient(
    credential=credential,
    subscription_id=AML_SUBSCRIPTION_ID,
    resource_group_name=AML_RESOURCE_GROUP,
    workspace_name=AML_WORKSPACE,
)

# do something useless that requires making requests
jobs = client.jobs.list()
_ = [j.name for j in jobs]

I would expect that if I run this script, authenticate, and then run it a second time, it would not request authentication again. However, it does.

I have tried different credential scopes, as well creating and manually serializing and deserializing all manner of “tokens” and “authorization records”, and passing those tokens and authorization records into methods that may or may not actually do anything at all with them, but nothing works. I have scoured the documentation for the Python SDK. I contacted Azure, who were useless.

Does anyone have suggestions?

The solution was two parts:

  1. Use AuthenticationRecord
  2. make sure when you create the AuthenticationRecord it has the right scope

Working (minimal) example:

import os

from azure.ai.ml import MLClient
from azure.identity import DeviceCodeCredential, TokenCachePersistenceOptions, AuthenticationRecord

from .constants import AML_SUBSCRIPTION_ID, AML_RESOURCE_GROUP, AML_WORKSPACE


cache_path = os.path.expanduser("~/.azure/msal_token_cache.json")
auth_record_path = os.path.expanduser("~/.azure/auth_record.json")

token_cache_options = TokenCachePersistenceOptions(name=cache_path, allow_unencrypted_storage=True)
if os.path.exists(auth_record_path):
    print(f"Using cache: {auth_record_path}")
    with open(auth_record_path, 'r') as auth_in:
        record_json_str = auth_in.read()
        deserialized_record = AuthenticationRecord.deserialize(record_json_str)
    credential = DeviceCodeCredential(cache_persistence_options=token_cache_options, authentication_record=deserialized_record)
else:
    print(f"Cache not found, authenticating")
    credential = DeviceCodeCredential(cache_persistence_options=token_cache_options)
    record = credential.authenticate(scopes=["https://ml.azure.com/.default"])
    record_json = record.serialize()
    with open(auth_record_path, 'w') as auth_out:
        auth_out.write(record_json)


client = MLClient(
    credential=credential,
    subscription_id=AML_SUBSCRIPTION_ID,
    resource_group_name=AML_RESOURCE_GROUP,
    workspace_name=AML_WORKSPACE,
)

# do something useless that requires making requests
jobs = client.jobs.list()
_ = [j.name for j in jobs]

print("Successfully ran the script")

More info can be found here

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật