VBA tải file có yêu cầu đăng nhập
Thân gửi các anh chị trong diễn đàn,
Cũng liên quan đến chủ đề URLToDownloadFile như trong chủ đề này trên diễn đàn: www.giaiphapexcel.com/diendan/threads/li%E1%BB%87u-h%C3%A0m-urldownloadtofile-c%C3%B3-th%E1%BB%83-t%E1%BA%A3i-file-v%E1%BB%81-khi-%C4%91%C6%B0%E1%BB%9Dng-d%E1%BA%ABn-c%C3%B3-chu%E1%BB%97i-unicode.67281/
Mình phải tải file được chia sẻ trong mạng SharePoint của công ty và phải đăng nhập trình duyệt mới tải được. Với cách thức download như chủ đề trên chỉ có thể download dạng file được chia sẻ cho bất kỳ ai có liên kết không phải đăng nhập.
Nên xin thỉnh giáo các anh chị phương cách nào có thể tải được file yêu cầu đăng nhập trước, hoặc code nào có sử dụng trình duyệt đã đăng nhập để tải về được.
Xin cảm ơn!
Lấy link API của sharepoint và user + password là tải về được.
Cảm ơn bạn đã định hướng. Bạn có thể hướng dẫn chi tiết hơn được không?
Lấy link API, kết nối tới SharePoint theo user + password, giữ phiên làm việc.
Kết nối tới link file trong SharePoint cần lấy rồi tải về thôi.
Ngoài ra, tạo folder, upload file lên SharePoint được luôn.
Hiện tại là sử dụng Microsoft Graph API.
Viết macro xác thực OAuth2 theo hướng dẫn tại đây, có thể sử dụng thư viện WinHTTPRequest hoặc XMLHTTPRequest:
[URL='learn.microsoft.com/en-us/graph/auth-v2-user']Get access on behalf of a user
Sau khi đã có được Access Token thì viết macro theo hướng dẫn:
[URL='learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http']Download the contents of a DriveItem
Ví dụ (lưu ý macro dưới đây chỉ là minh họa):
Public Sub DownloadFile(SiteId As String, FolderPath As String, FileName As String, DestinationFolder As String)
Dim objWinHttp As WinHttp.WinHttpRequest
Dim objRandom As Random
Dim decCurrentPos As Variant, decChunkSize As Variant, decTotalSize As Variant
Dim objJson As Scripting.Dictionary
Set objWinHttp = New WinHttp.WinHttpRequest
With objWinHttp
.Open "GET", "graph.microsoft.com/v1.0/sites/" & SiteId & "/drive/items/root:/" & FolderPath & "/" & FileName & "?$select=size", True
.SetRequestHeader "Authorization", "Bearer " & ActiveService.AccessToken
.Send
.WaitForResponse
If .Status = 200 Then
Set objJson = JsonConverter.ParseJson(.ResponseText)
decTotalSize = CDec(objJson.Item("size"))
Else: Call HandleError(.ResponseText)
End If
End With
Set objWinHttp = Nothing
Set objWinHttp = New WinHttp.WinHttpRequest
decChunkSize = CDec(decChunkSize)
decChunkSize = 10485760
decCurrentPos = CDec(decCurrentPos)
decCurrentPos = 0
While decTotalSize - decCurrentPos > 0
Set objWinHttp = New WinHttp.WinHttpRequest
With objWinHttp
.Open "GET", "graph.microsoft.com/v1.0/sites/" & SiteId & "/drive/items/root:/" & FolderPath & "/" & FileName & ":/content", True
If decTotalSize - decCurrentPos > 0 Then
.SetRequestHeader "Range", "bytes=" & CStr(decCurrentPos) & "-" & CStr(decCurrentPos + decChunkSize - 1)
Else: .SetRequestHeader "Range", "bytes=" & CStr(decCurrentPos) & "-" & CStr(decTotalSize - 1)
End If
.SetRequestHeader "Authorization", "Bearer " & ActiveService.AccessToken
.SetRequestHeader "Content-Type", "application/octet-stream"
.Send
.WaitForResponse
If .Status = 206 Then
Set objRandom = New Random
objRandom.OpenFile DestinationFolder & "" & FileName, ForAppending
objRandom.WriteBytes .ResponseBody
objRandom.CloseFile
Set objRandom = Nothing
decCurrentPos = decCurrentPos + decChunkSize
Else: Call HandleError(.ResponseText)
End If
End With
Set objWinHttp = Nothing
Wend
End Sub
Private Function BytesToMegabytes(Bytes As Double) As Double
BytesToMegabytes = Bytes / 1024 / 1024
End Function
Private Sub HandleError(Source As String)
Dim objJson As Scripting.Dictionary
Set objJson = JsonConverter.ParseJson(Source)
Err.Raise vbObjectError, objJson.Item("error")("code"), objJson.Item("error")("message")
End Sub
www.giaiphapexcel.com/diendan/threads/vba-t%E1%BA%A3i-file-c%C3%B3-y%C3%AAu-c%E1%BA%A7u-%C4%91%C4%83ng-nh%E1%BA%ADp.160459/
Xây dựng Lương 3P, KPI cho Doanh nghiệp
Làm thế nào để trả lương cho nhân viên chính xác nhất? Đây là một trong những câu hỏi khó trong quản trị nhân...
Xem khóa học
Bình luận