以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 调整C#转换过来的代码 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=191096) |
-- 作者:lxhmax -- 发布时间:2024/3/27 15:57:00 -- 调整C#转换过来的代码 老师,下面这段代码是在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 |
-- 作者:有点蓝 -- 发布时间:2024/3/27 16:15:00 -- 把RestSharp这个第三方组件复制到Foxtable的安装目录,注意这个组件要支持net framework 4.0 1、去掉【Namespace wayfair_integration】这一行及前面所有代码,去掉最后一行2、Class GraphiQL_Code_Template改为public Class GraphiQL_Code_Template 3、Private 全部改为public 4、把命名空间添加到代码里,比如 public Shared Function SendRequest(ByVal method As RestSharp.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 RestSharp.IRestResponse Dim client As New RestSharp.RestClient(url) |
-- 作者:lxhmax -- 发布时间:2024/3/27 16:38:00 -- 老师,下面是我取的部分代码,这么改对吗? 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) |
-- 作者:有点蓝 -- 发布时间:2024/3/27 16:55:00 -- 红色这个我也不清楚,去看组件的文档吧 提示错误的这个加上命名空间,参考2楼第4点
|
-- 作者:lxhmax -- 发布时间:2024/3/27 17:40:00 -- 下面转换的VB代码 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编辑过]
|
-- 作者:有点蓝 -- 发布时间:2024/3/28 8:28:00 -- 加上命名空间 |