0

I am new to using MS Graph and the new version of RestSharp. I put together a simple console app in VB.NET to learn the process. Have spent way too much time looking at solutions on the internet, but not getting the answers I want. The ultimate goal is to upload files to a OneDrive account autonomously. So I created an App registration on Azure for this app. Then to test I created a console app to test how the process works. I am using the latest version of RestSharp which is very different from the version I was using. The console app calls a simple subroutine shown below to get the token to make calls to Graph. When I run I get the errors: "System.Net.ProtocolViolationException' in System.dll" and "System.Net.ProtocolViolationException' in mscorlib.dll". Code follows:

        Dim client As New RestClient("https://login.microsoftonline.com/f8cde-----/oauth2/v2.0/token")
        Dim request As New RestRequest(Method.Post)
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
        request.AddHeader("Host", "login.microsoftonline.com")

        Dim body As String = "client_id=8e4---------" &
                "scope=api://8e4da056-8425-4-.default" &
                "client_secret=Piy7Q~7----------" &
                "grant_type=client_credentials"
        request.AddBody(body)
        Dim response As RestSharp.RestResponse = Await client.ExecuteAsync(request)
        Console.Write(response.Content.ToString)
        Console.ReadKey()
    End Sub

I admit I really don't know what I am doing here, but want to learn.

DougM
  • 109
  • 3
  • 16
  • I got rid of the error by changing the body to Dim body As String = "client_id=8e4---------&" & "scope=api://8e4da056-8425-4-.default&" & "client_secret=Piy7Q~7----------&" & "grant_type=client_credentials". But call returns nothing – DougM Mar 17 '22 at 23:00

1 Answers1

0

I can suggest spending time reading the documentation. RestSharp forms the request correctly if you use the RestRequest API to build the request.

Here is what you need to do:

Dim options = New RestClientOptions("https://login.microsoftonline.com/f8cde-----/oauth2/v2.0/token")
options.BaseHost = "login.microsoftonline.com"
Dim client = New RestClient(options)

Dim request As New RestRequest(Method.Post)
request.AddParameter("client_id", "8e4---------")
request.AddParameter("client_secret", "Piy7Q~7----------")
request.AddParameter("grant_type", "client_credentials")
Dim response = Await client.ExecuteAsync(request)
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83