Foxtable(狐表)用户栏目专家坐堂 → 调整C#转换过来的代码


  共有306人关注过本帖平板打印复制链接

主题:调整C#转换过来的代码

帅哥哟,离线,有人找我吗?
lxhmax
  1楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:621 积分:5549 威望:0 精华:0 注册:2012/8/2 19:04:00
调整C#转换过来的代码  发帖心情 Post By:2024/3/27 15:57:00 [只看该作者]

老师,下面这段代码是在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


 回到顶部