Using the Microsoft Graph API to load files from Sharepoint into Databricks

I want to load files from Sharepoint using a Microsoft App registration and the Microsoft Graph API. Right now I get the error that I am not authotized but I do not know if I just do not have the necessary permissions or if I did something wrong about the path of the file I want to load. I have the Sites.Read.All and Sites.Select permissions. Can anyone help me? My code looks like this:

sharepoint_site = "company_name.sharepoint.com/sites/site_name"
sharepoint_file_path = "relative_path/file.xlsx"
client_id = dbutils.secrets.get("secret_scope", "client_id")
tenant_id = dbutils.secrets.get("secret_scope", "tenant_id")
client_secret = dbutils.secrets.get("secret_scope", "client_secret")

auth_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
auth_data = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://graph.microsoft.com/.default"
}

auth_response = requests.post(auth_url, data=auth_data)
if auth_response.status_code != 200:
    print("Fehler bei der Authentifizierung")
    print(auth_response.text)
    exit()

access_token = auth_response.json().get("access_token")

headers = {"Authorization": f"Bearer {access_token}"}
download_url = f"https://graph.microsoft.com/v1.0/sites/{sharepoint_site}/drive/root:/{sharepoint_file_path}:/content"
file_response = requests.get(download_url, headers=headers)
if file_response.status_code == 200:
    file_path = "/dbfs/path.xlsx"
    with open(file_path, "wb") as f:
        f.write(file_response.content)
    print("Datei heruntergeladen!")
else:
    print(f"Fehler: {file_response.status_code}")
    print(file_response.text)

New contributor

yannik0103 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Grant Files.Read.All api permission to the application with Application permission type.

After getting access token make sure you are having roles,
you check this in jwt.io by pasting the token.

Next, you get the site id using below code.

headers = {"Authorization": f"Bearer {access_token}"}

site_res = requests.get("https://graph.microsoft.com/v1.0/sites/root:/sites/<site_name>",headers=headers)
site_id = site_res.json()['id']

Then you make request to get the file with below code.

download_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:/Identity AR.xlsx:/content"

file_response = requests.get(download_url, headers=headers)
print(file_response.status_code)
if file_response.status_code == 200:
    file_path = "/dbfs/identity.xlsx"
    with open(file_path, "wb") as f:
        f.write(file_response.content)
    print(f"file created")
else:
    print(file_response.status_code)

Recognized by Microsoft Azure Collective

4

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