I run the code below and I get a 403 Forbidden error when it gets to Dim rspe As Net.WebResponse = wr.GetResponseat the end. It not only happens with trying to resume the upload session, but also when I try to just check the status of the session. The code below needs a reference to System.Web.Extensions added to the project in order to run. The other things that need to be changed have the word REDACTED in their places. I've read a few other forums that suggest that this is just an issue with OneDrive for Business using App level tokens. The session gets created successfully and a temp file is created in the destination on OneDrive.
Imports System.IO
Imports System.Web.Script.Serialization
ModuleModule1
Private dtAccessTokenAsDateTime ="1/1/1900"
Private strAccessTokenAsString =""
Private strUserNameAsString ="REDACTED"
Private strUploadFileAsString ="C:\Temp\test.pdf"
Private strTokenURLAsString ="https://login.microsoftonline.com/REDACTED/oauth2/v2.0/token"
Private strAppSecretAsString ="client_id=REDACTED&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=REDACTED&grant_type=client_credentials"
Sub Main()
GetAccessToken()
Dim driveIDAsString = GetDriveID(strAccessToken, strUserName)
Dim strURLAsString ="https://graph.microsoft.com/beta/drives/"& driveID &"/root:/test.pdf:/createUploadSession"
Dim rspAsString =""
Dim wcAsNew Net.WebClient()
wc.Headers.Add("Authorization","Bearer "& strAccessToken)
wc.Headers.Add("Content-Type","application/json")
rsp = wc.UploadString(strURL, "POST","{""item"": {""@microsoft.graph.conflictBehavior"": ""rename""}}")
Dim jssAsNewJavaScriptSerializer
Dim objRspAsObject = jss.Deserialize(OfObject)(rsp)
Dim strUploadUrlAsString =""
If objRsp.TryGetValue("uploadUrl", strUploadUrl)Then
Dim strmAsNew IO.FileStream(strUploadFile,FileMode.Open)
Dim iChunkAsInteger = 3145728
Dim iLenAsInteger = strm.Length
Dim byt(iLen)AsByte
strm.Read(byt, 0, strm.Length)
Dim wrAs Net.HttpWebRequest = Net.WebRequest.Create(strUploadUrl)
wr.ContentLength = iLen
wr.Headers.Add("Content-Range","bytes 0-"& iLen - 1 &"/"& iLen)
wr.Method = "PUT"
Console.WriteLine("bytes 0-"& iLen - 1 &"/"& iLen)
Dim rsAs IO.Stream = wr.GetRequestStream
rs.Write(byt, 0, iLen)
Dim rspeAs Net.WebResponse = wr.GetResponse
rs.Close()
EndIf
EndSub
PrivateSub GetAccessToken()
Dim wcAsNew Net.WebClient()
wc.Headers.Add("Content-Type","application/x-www-form-urlencoded")
Dim strReturn = wc.UploadString(strTokenURL, strAppSecret)
Dim jssAsNewJavaScriptSerializer
Dim objRetAsObject = jss.Deserialize(OfObject)(strReturn)
strAccessToken = objRet("access_token").Replace(vbCrLf,"").Replace(vbNewLine,"")
dtAccessToken = Now()
EndSub
PrivateFunction GetDriveID(strTokenAsString, strUserAsString)AsString
Dim strDriveIDAsString =""
Try
Dim wcAsNew Net.WebClient()
wc.Headers.Add("Authorization","Bearer "& strToken)
Dim strResponseAsString = wc.DownloadString("https://graph.microsoft.com/v1.0/users/"& strUser & "/drives")
Dim jssAsNewJavaScriptSerializer
Dim objContentsAsObject = jss.Deserialize(OfObject)(strResponse)
ForEach obj AsDictionary(OfString, Object)In objContents("value")
Dim strContentAsObject =Nothing
If obj.TryGetValue("id", strContent)Then
strDriveID = obj.Item("id")
EndIf
Next
Catch exAsException
EndTry
Return strDriveID
EndFunction
EndModule