老师,下面这段代码是在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
老师,下面是我取的部分代码,这么改对吗?
Public Class GraphiQL_Code_Template
Public 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 qvjtdxwOZVJTzeBF0xxUtJlpn52UwuiJyoasKyMfKzx7L0iTXwLAc")
'Dim graphQLOperation As String = $" ""query"": ""{query}"", ""variables"": {variables}"
'Dim graphQLPayload As String = $"{{{graphQLOperation}}}"
上面的蓝色是源代码,我改成下面这样对吗?
Dim graphQLOperation As String = "query: {id,userType,username,firstName,lastName,email},variables: {null}"
Dim graphQLPayload As String = "query: {id,userType,username,firstName,lastName,email},variables: {null}"
Try
Dim response As IRestResponse = SendRequest(Method.POST, "https://api.wayfair.com/v1/graphql", "application/json", graphQLPayload, headers)
Return JObject.Parse(response.Content)
Catch e As Exception
Console.WriteLine($"Problem executing the GraphQL request: {e.Message}")
System.Windows.Forms.TextBox.Environment.[Exit](1)
End Try
Return Nothing
End Function
End Class保存的时候提示编译错误:未定义类型"IRestResponse”
锴误代码:Dim response As lRestResponse =SendRequest(Method.PosT,"https://api.wayfair.com/v1/graphql"application/json",graphQLPayload, headers)
Private Sub SurroundingSub()
Dim client = New RestClient("https://api.wayfair.com/v1/graphql")
client.Timeout = -1
Dim request = New RestRequest(Method.POST)
request.AddHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsImt")
request.AddHeader("Cache-Control", "no-cache")
client.UserAgent = "Apifox/1.0.0 (https://apifox.com)"
request.AddHeader("Content-Type", "application/json")
request.AddHeader("Accept", "*/*")
request.AddHeader("Host", "api.wayfair.com")
request.AddHeader("Connection", "keep-alive")
request.AddParameter("application/json", "{""query"":""query {\r\n identity {\r\n username,\r\n email,\r\n applications {\r\n name\r\n }\r\n }\r\n}"",""variables"":{}}", ParameterType.RequestBody)
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.Content)
End Sub
报错如下,要怎么处理?
编译错误:未定义类型~RestClient”
错退代码:Dim client = NewRestclient("https://api.wayfair.com/v1/graphql")
[此贴子已经被作者于2024/3/27 17:43:00编辑过]