【问题标题】:How do I authenticate to the GoDaddy API using C#?如何使用 C# 向 GoDaddy API 进行身份验证?
【发布时间】:2016-03-17 15:15:04
【问题描述】:

我已在 GoDaddy 上设置了一个帐户,并拥有用于访问 API 的开发人员密钥。使用 Fiddler,我可以构造一个返回结果的请求。但是,使用控制台应用程序中的以下代码失败并显示“未经授权”。我在两个地方都使用相同的地址和密钥。

我错过了什么?

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "sso-key VUjHMntw_UyosKRMGaLXE4e3E1h29Xx:DSqM2jiJcRyXvSbLehjYUZ");

            HttpResponseMessage response = await client.GetAsync("https://api.ote-godaddy.com/v1/domains/available?domain=google.com");

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync<string>();
                Console.WriteLine(result);
            }
            else
            {
                Console.WriteLine(response.ReasonPhrase);
            }
        }

注意:授权密钥和秘密已被修改。

以下是我在 Fiddler 中所做的工作:

【问题讨论】:

    标签: c# httpclient godaddy-api


    【解决方案1】:

    我相当确定您将身份验证标头发送为:

    Authorization: Authorization sso-key VUjHMntw_UyosKRMGaLXE4e3E1h29Xx:DSqM2jiJcRyXvSbLehjYUZ
    

    试试这个:

    client.DefaultRequestHeaders.Authorization = 
        new System.Net.Http.Headers.AuthenticationHeaderValue("sso-key", "VUjHMntw_UyosKRMGaLXE4e3E1h29Xx:DSqM2jiJcRyXvSbLehjYUZ");
    

    Authorization: 前缀由方法调用分配给您。

    【讨论】:

    • 谢谢!!这个王者也不起作用: new AuthenticationHeaderValue($"sso-key {key}:{secret}");
    【解决方案2】:

    该特定调用不需要身份验证。这应该适合你。

        static void Main(string[] args)
        {
            TestDomain().Wait();
        }
    
        public static async Task<string> TestDomain()
        {
    
            using (var client = new HttpClient())
            {
                //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "sso-key VUjHMntw_UyosKRMGaLXE4e3E1h29Xx:DSqM2jiJcRyXvSbLehjYUZ");
    
                HttpResponseMessage response = await client.GetAsync("https://api.ote-godaddy.com/v1/domains/available?domain=google.com");
    
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(result);
    
                    return result;
                }
                else
                {
                    Console.WriteLine(response.ReasonPhrase);
    
                    return response.ReasonPhrase;
    
                }
            }
    
        }
    

    【讨论】:

    • 没错。我应该使用不同的 api 调用来演示问题。
    【解决方案3】:

    ' ' 视觉基础 ' 控制台应用程序 ' 注册 GoDaddy(这似乎只适用于 PRODUCTION 密钥、秘密和 URL ' ' 示例使用 ' 您想在家中托管一个域名(例如 ISP = Cox Cable),但您有一个动态 IP 地址,而不是静态 IP 地址 ' 大约每分钟调用一次此例程(GoDaddy 每小时限制 60 次),它将更改 GoDaddy 的 A 记录以指向您家中的新 IP '

    模块模块1

    Const DEFAULT_IP_ADDRESS As String = "0.0.0.0"
    Const GODADDYKEY_AND_SECRET As String = "GoDaddyKeyGoesHere:GoDaddySecretGoesHere"  'key:secret  Get this from GoDaddy API.  Use Production (not test)
    Public objGoDaddyResult As Object = Nothing
    
    Public vbWhack As String = "\"
    
    Sub Main()
        Dim sCurrentCoxIPAddress As String
        Dim sCurrentARecordIPAddress As String
    
        sCurrentCoxIPAddress = GetExternalIPMain()          ' Calls up to three "services" to get you public-facing IP address
    
        If sCurrentCoxIPAddress <> DEFAULT_IP_ADDRESS Then
            Console.WriteLine(sCurrentCoxIPAddress)
            ' Call GoDaddy API to get what the current A Record is set to
            GetGoDaddyARecord().Wait()
            sCurrentARecordIPAddress = ghExtract(Trim(objGoDaddyResult.ToString), ghEscape("\034Data\034:\034"), ghEscape("\034,\034name\034"))
            Console.WriteLine(sCurrentARecordIPAddress)
    
            If sCurrentCoxIPAddress = sCurrentARecordIPAddress Then
                Console.WriteLine("IP Addresses match, nothing to do")
            Else
                Console.WriteLine("Cox dynamically changed my IP.  Changed A Record at GoDaddy")
                'Call GoDaddy API to Change A Record.
                PutGoDaddyARecord(sCurrentCoxIPAddress).Wait()
            End If
            Console.WriteLine(objGoDaddyResult)
        Else
            Console.WriteLine("Could not find IP address after looking at three Websites")
        End If
    
        Console.ReadKey()
        End
    
    End Sub
    
    Private Async Function GetGoDaddyARecord() As Task(Of String)
        Dim MyWebClient = New Net.Http.HttpClient()
        Dim msgResponseMessage As Net.Http.HttpResponseMessage
    
        Const GODADDY_API_CALL As String = "https://api.godaddy.com/v1/domains/" & YOUR_DOMAIN_NAME_HERE & "/records/A/@"
    
        MyWebClient.DefaultRequestHeaders.Authorization = New Net.Http.Headers.AuthenticationHeaderValue("sso-key", GODADDYKEY_AND_SECRET)
        msgResponseMessage = Await MyWebClient.GetAsync(GODADDY_API_CALL)
    
        If msgResponseMessage.IsSuccessStatusCode = True Then
            objGoDaddyResult = Await msgResponseMessage.Content.ReadAsStringAsync()
        Else
            objGoDaddyResult = msgResponseMessage.ReasonPhrase
        End If
    
        Return objGoDaddyResult
    
    End Function
    
    
    Private Async Function PutGoDaddyARecord(ByVal strNewARecord As String) As Task(Of String)
        Dim sGoDaddyData As String
        Dim wcWebClient = New Net.Http.HttpClient()
        Dim scStringContent As Net.Http.StringContent
        Dim msgGoDaddyResponse As Net.Http.HttpResponseMessage
        Dim uriMyURI As Uri
    
        Const GODADDY_API_CALL As String = "https://api.godaddy.com/v1/domains/" & YOUR_DOMAIN_NAME_HERE & "/records/A/@"
        Const GODADDY_DATA As String = "[{\034data\034:\034[[NEW_A_RECORD]]\034}]"
        sGoDaddyData = Replace(GODADDY_DATA, "[[NEW_A_RECORD]]", strNewARecord,,, CompareMethod.Text)
        sGoDaddyData = ghEscape(sGoDaddyData)
        uriMyURI = New Uri(GODADDY_API_CALL)
        scStringContent = New Net.Http.StringContent(sGoDaddyData, Text.UnicodeEncoding.UTF8, "application/json")
    
        wcWebClient.DefaultRequestHeaders.Authorization = New Net.Http.Headers.AuthenticationHeaderValue("sso-key", GODADDYKEY_AND_SECRET)
        msgGoDaddyResponse = Await wcWebClient.GetAsync(GODADDY_API_CALL)
    
        If msgGoDaddyResponse.IsSuccessStatusCode = True Then
            msgGoDaddyResponse = Await wcWebClient.PutAsync(uriMyURI, scStringContent)
            objGoDaddyResult = msgGoDaddyResponse.ReasonPhrase
        Else
            objGoDaddyResult = msgGoDaddyResponse.ReasonPhrase
        End If
    
        Return objGoDaddyResult
    
    End Function
    
    '
    ' Calls three websites
    ' This is more a robust approach because it scrapes somebody else's website and we have no control over their future changes.
    '
    Private Function GetExternalIPMain() As String
        Dim sReturn As String = DEFAULT_IP_ADDRESS
    
        sReturn = GetExternalIP1()
        If sReturn = DEFAULT_IP_ADDRESS Then
            sReturn = GetExternalIP2()
            If sReturn = DEFAULT_IP_ADDRESS Then
                sReturn = GetExternalIP3()
                If sReturn = DEFAULT_IP_ADDRESS Then
                    'Console.ReadKey()
                End If
            End If
        End If
    
        Return Trim(sReturn)
    End Function
    
    
    ' [1 of 3] feron.it
    Private Function GetExternalIP1() As String
        Const WEB_PAGE_WITH_MYIP As String = "http://tools.feron.it/php/ip.php"
        Dim sReturn As String = DEFAULT_IP_ADDRESS
        Dim MyWebClient As Net.WebClient = New Net.WebClient()
    
        Try
            sReturn = MyWebClient.DownloadString(WEB_PAGE_WITH_MYIP)
        Catch ex As Exception
    
        End Try
    
        Return sReturn
    End Function
    
    ' [2 of 3] dyndns.org
    Private Function GetExternalIP2() As String
        Const WEB_PAGE_WITH_MYIP As String = "http://checkip.dyndns.org/"
        Const BEGIN_SCRAPE_STRING As String = "Current IP Address: "
        Const END_SCRAPE_STRING As String = "</body>"
    
        Dim sReturn As String = DEFAULT_IP_ADDRESS
        Dim MyWebClient As Net.WebClient = New Net.WebClient()
        Dim sPageContents As String = ""
    
        Try
            sPageContents = MyWebClient.DownloadString(WEB_PAGE_WITH_MYIP)
        Catch ex As Exception
    
        End Try
    
        If Len(sPageContents) > 0 Then
            sReturn = ghExtract(sPageContents, BEGIN_SCRAPE_STRING, END_SCRAPE_STRING)
        End If
    
        Return sReturn
    End Function
    
    ' [3 of 3] ap-adress.com
    Private Function GetExternalIP3() As String
        Const WEB_PAGE_WITH_MYIP As String = "http://www.ip-adress.com/"
        Const BEGIN_SCRAPE_STRING As String = "Your IP address is: <strong>"
        Const END_SCRAPE_STRING As String = "</strong>"
    
        Dim sReturn As String = DEFAULT_IP_ADDRESS
        Dim MyWebClient As Net.WebClient = New Net.WebClient()
        Dim sPageContents As String = ""
    
        Try
            sPageContents = MyWebClient.DownloadString(WEB_PAGE_WITH_MYIP)
        Catch ex As Exception
    
        End Try
    
        If Len(sPageContents) > 0 Then
            sReturn = ghExtract(sPageContents, BEGIN_SCRAPE_STRING, END_SCRAPE_STRING)
        End If
    
        Return sReturn
    End Function
    
    ' Function I like
    Private Function ghExtract(ByVal strString As String, ByVal strBegin As String, ByVal strEnd As String) As String
        Dim sReturn As String = ""
    
        Dim iTempBegin As Integer = InStr(strString, strBegin, CompareMethod.Text) + Len(strBegin)
        Dim iTempEnd As Integer = InStr(strString, strEnd, CompareMethod.Text)
        If iTempEnd > iTempBegin Then
            sReturn = Strings.Mid(strString, iTempBegin, iTempEnd - iTempBegin)
        End If
    
        Return sReturn
    
    End Function
    
    ' Function I like
    Public Function ghEscape(ByVal strString As String) As String
        Dim i As Integer
        If ghInstrBinary(strString, vbWhack) = True Then
            For i = 1 To 254
                strString = Replace(strString, vbWhack & Strings.Right("00" & i.ToString, 3), Chr(i),,, CompareMethod.Binary)
            Next
        End If
        Return strString
    End Function
    
    ' Function I like
    Function ghInstrBinary(ByVal strString As String, strSearchString As String) As Boolean
        Dim bReturn As Boolean = False
        If InStr(strString, strSearchString, CompareMethod.Binary) > 0 Then
            bReturn = True
        End If
        Return bReturn
    End Function
    

    结束模块

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 2016-08-30
      • 2019-08-22
      • 2013-06-21
      • 2021-07-15
      • 2023-03-13
      • 2022-11-08
      相关资源
      最近更新 更多