老师,下面这段代码是在https://converter.telerik.com/转换过来的代码,放到全局代码保存保存,请问下要怎么调整下?
Imports System
Imports System.Net
Imports System.Collections.Generic
Imports RestSharp
Imports Newtonsoft.Json.Linq
Namespace wayfair_integration
Class GraphiQL_Code_Template
Shared CLIENT_ID As String = "{YOUR_CLIENT_ID}"
Shared CLIENT_SECRET As String = "{YOUR_CLIENT_SECRET}"
Shared API_URL As String = "https://api.wayfair.com/v1/graphql"
Shared AUTH_URL As String = "https://sso.auth.wayfair.com/oauth/token"
Shared QUERY As String = "query identity {identity {id,userType,username,firstName,lastName,email}}"
Shared VARIABLES As String = "null"
Private Shared Function SendRequest(ByVal method As Method, ByVal url As String, ByVal Optional bodyContentType As String = "", ByVal Optional body As String = "", ByVal Optional headers As Dictionary(Of String, String) = Nothing) As IRestResponse
Dim client As RestClient = New RestClient(url)
Dim request As RestRequest = New RestRequest(method)
If headers IsNot Nothing Then
For Each header As KeyValuePair(Of String, String) In headers
request.AddHeader(header.Key, header.Value)
Next
End If
If method = Method.POST Then
request.AddParameter(bodyContentType, body, ParameterType.RequestBody)
End If
Dim response As IRestResponse = client.Execute(request)
Dim responseStatus As HttpStatusCode = response.StatusCode
Select Case responseStatus
Case HttpStatusCode.OK
Return response
Case Else
Throw New Exception($"Request failed with status {response.StatusDescription}, response {response.Content}")
End Select
End Function
Private Shared Function SendGraphQLRequest(ByVal token As String, ByVal query As String, ByVal variables As String) As JObject
Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)()
headers.Add("content-type", "application/json")
headers.Add("cache-control", "no-cache")
headers.Add("authorization", $"Bearer {token}")
Dim graphQLOperation As String = $" ""query"": ""{query}"", ""variables"": {variables}"
Dim graphQLPayload As String = $"{{{graphQLOperation}}}"
Try
Dim response As IRestResponse = SendRequest(Method.POST, API_URL, "application/json", graphQLPayload, headers)
Return JObject.Parse(response.Content)
Catch e As Exception
Console.WriteLine($"Problem executing the GraphQL request: {e.Message}")
System.Environment.[Exit](1)
End Try
Return Nothing
End Function
Private Shared Function FetchToken(ByVal clientID As String, ByVal clientSecret As String) As String
Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)()
headers.Add("content-type", "application/json")
headers.Add("cache-control", "no-cache")
Dim authCredentials As String = $"
""grant_type"": ""client_credentials"",
""client_id"": ""{clientID}"",
""client_secret"": ""{clientSecret}"",
""audience"": ""https://api.wayfair.com/""
"
Dim authPayload As String = $"{{{authCredentials}}}"
Try
Dim response As IRestResponse = SendRequest(Method.POST, AUTH_URL, "application/json", authPayload, headers)
Return CStr(JObject.Parse(response.Content)("access_token"))
Catch e As Exception
Console.WriteLine($"Could not retrieve a token for the request: {e.Message}")
System.Environment.[Exit](1)
End Try
Return ""
End Function
Private Shared Sub Main(ByVal args As String())
Dim myToken As String = FetchToken(CLIENT_ID, CLIENT_SECRET)
Dim graphQLResponse As JObject = SendGraphQLRequest(myToken, QUERY, VARIABLES)
Console.WriteLine(graphQLResponse)
End Sub
End Class
End Namespace