获取承载者令牌-Oauth 2

时间:2019-06-01 11:55:08

标签: .net api oauth-2.0

目标是从此API接收承载令牌。我已经尝试通过Postman进行尝试,并且能够成功接收到它,但是希望通过我的应用程序实现自动化。

我正在连接到第三方应用程序,并且对该API进行调用的要求是使用Oauth 2连接。必填字段是clientID,ClientSecret,grantType,authURL,UserName,Password和redirectURI。我尝试了很多选择,但以下情况是我最近的选择。使用以下脚本时,我得到一个StatusCode:400错误的请求。

Private Shared Function GetToken(ByVal userName As String, ByVal password As String) As String
        Dim pairs = New List(Of KeyValuePair(Of String, String)) From {
        New KeyValuePair(Of String, String)("client_id", "ClientID"),
        New KeyValuePair(Of String, String)("client_secret", "ClientSecret"),
        New KeyValuePair(Of String, String)("grant_type", "authorization_code"),
        New KeyValuePair(Of String, String)("auth_url", "https://url/authorization"),
        New KeyValuePair(Of String, String)("username", userName),
        New KeyValuePair(Of String, String)("password", password),
        New KeyValuePair(Of String, String)("redirect_uri", "http://localhost:5000/auth/callback")
    }
        Dim content = New FormUrlEncodedContent(pairs)

        Using client = New HttpClient()
            Dim response = client.PostAsync("https://url/token", content).Result
            Return response.Content.ReadAsStringAsync().Result
        End Using
   End Function

0 个答案:

没有答案