通过OAuth访问imgUr(上传到用户帐户)

时间:2014-06-11 07:07:29

标签: .net vb.net api file-upload imgur

开始这样做"简单"任务我已经研究了一个程序,我已经把它作为一个例子here来跟踪和重现这些步骤,程序可以上传图像"匿名":

Private ReadOnly ClientId As String = "My Client ID" ' => "..............."
Private ReadOnly ClientSecret As String = "My Client Secret" ' => "........................................"

' Usage:
' Dim url As String = UploadImage("C:\Image.jpg") : MessageBox.Show(url)
Public Function UploadImage(ByVal image As String)

    Dim w As New WebClient()
    w.Headers.Add("Authorization", "Client-ID " & ClientId)
    Dim Keys As New System.Collections.Specialized.NameValueCollection

    Try

        Keys.Add("image", Convert.ToBase64String(File.ReadAllBytes(image)))
        Dim responseArray As Byte() = w.UploadValues("https://api.imgur.com/3/image", Keys)
        Dim result = Encoding.ASCII.GetString(responseArray)
        Dim reg As New System.Text.RegularExpressions.Regex("link"":""(.*?)""")
        Dim match As Match = reg.Match(result)
        Dim url As String = match.ToString.Replace("link"":""", "").Replace("""", "").Replace("\/", "/")
        Return url

    Catch s As Exception

        MessageBox.Show("Something went wrong. " & s.Message)
        Return "Failed!"

    End Try

End Function

但我真正想要的是将图片上传到我的用户帐户http://elektrostudios.imgur.com

我发现了this问题,但他在回答中说的不清楚(由于我的新手知识),无论如何我试图使用上述功能但只是发送带有BEARER ID&{39}的ClientSecret标题如果我理解oauth 2 api documentation说明令牌也可以是ClientSecret ID?,但我不是&# 39;得到预期的结果。

所以搜索获取正确访问令牌的方式我已经看过this其他问题,这有助于我发现RestSharp库并知道如何发送请求,我做了一些修改将它与Imgur API一起使用,但我得到了这个错误响应:

{"data":{"error":"client_id and response_type are required","request":"\/oauth2\/authorize","method":"POST"},"success":false,"status":400}

这就是我所拥有的:

Public Sub GetAccessToken()

    Dim xrc As RestClient = New RestClient
    Dim grant_type As String = "authorization_code"
    Dim request As New RestRequest(Method.POST)
    Dim strBody As String
    Dim response As RestResponse
    Dim strResponse As String

    request.Method = Method.POST
    request.RequestFormat = DataFormat.Xml

    'Base URL
    xrc.BaseUrl = "https://api.imgur.com"

    'Resource
    request.Resource = "oauth2/authorize"

    'Format body
    strBody = String.Format("client_id={0}&response_type={1}", ClientId, ClientSecret)

    'Add body to request
    request.AddBody("Authorization", strBody)

    'Execute
    response = xrc.Execute(request)

    'Parse Response
    strResponse = response.Content

    MessageBox.Show(response.Content.ToString)

End Sub

所以我的问题是2合1:

  

如何将图片上传到Imgur用户   帐户使用所需的东西,如访问令牌?。

PS:请记住,即使获得访问令牌,我也不知道如何在存储它之后使用它。

  

更新:

我尝试使用@ Plutonix 解决方案,但当我尝试请求令牌时,它会抛出异常" Need a valid PIN first",我&#39 ; m使用有效的ClientId和ClientSecret,我错过了更多的东西?,这里是代码:

Private imgUR As New imgurAPI("my client id", "my client secret")

Private Sub Button1_Click() Handles Button1.Click

    Dim wb As New WebBrowser
    imgUR.RequestPinBrowser(wb)

    ' The instruction below throws an exception:
    ' "Need a valid PIN first"
    Dim result As imgurAPI.imgUrResults = imgUR.RequestToken
    wb.Dispose()

    ' check result
    If result = imgurAPI.imgUrResults.OK Then

        ' assumes the file exists
        imgUR.UploadImage("C:\Test.jpg", False)

        Clipboard.SetText(imgUR.LastImageLink)
        MessageBox.Show(imgUR.LastImageLink)

    Else
        MessageBox.Show(String.Format("Error getting access token. Status:{0}",
            result.ToString))
    End If

End Sub

1 个答案:

答案 0 :(得分:6)

这很长,因为它或多或少是一个完整的API:

Public Class imgurAPI
    ' combination of this API and imgUR server responses
    Public Enum imgUrResults
        OK = 200                        ' AKA Status200 

        ' errors WE return
        OtherAPIError = -1              ' so far, just missing ImgLink
        InvalidToken = -2
        InvalidPIN = -3                 ' pins expire
        InvalidRequest = -4
        TokenParseError = -5

        ' results we get from server
        BadRequestFormat = 400          ' Status400   
        AuthorizationError = 401        ' Status401  

        Forbidden = 403                 ' Status403   
        NotFound = 404                  ' Status404   ' bad URL Endpoint
        RateLimitError = 429            ' Status429   ' RateLimit Error
        ServerError = 500               ' Status500   ' internal server error

        UknownStatus = 700              ' We havent accounted for it (yet), 
                                        '   may be trivial or new
    End Enum

    ' container for the cool stuff they send us
    Friend Class Token
        Public Property AcctUserName As String
        Public Property AccessToken As String
        Public Property RefreshToken As String
        Public Property Expiry As DateTime

        Public Sub New()
            AcctUserName = ""
            AccessToken = ""
            RefreshToken = ""
            Expiry = DateTime.MinValue
        End Sub

        Friend Function IsExpired() As Boolean

            If (Expiry > DateTime.Now) Then
                Return False
            Else
                ' if expired reset everything so some moron doesnt
                ' expose AccessToken and test for ""
                AcctUserName = ""
                AccessToken = ""
                RefreshToken = ""
                Expiry = DateTime.MinValue
                Return True
            End If
        End Function

    End Class

    ' NO simple ctor!!!
    ' constructor initialized with ClientID and SecretID
    Public Sub New(clID As String, secret As String)
        clientID = clID
        clientSecret = secret
        myPin = ""
        imgToken = New Token
        LastImageLink = ""
        UseClipboard = True
        AnonOnly = False
    End Sub

    ' constructor initialized with ClientID and SecretID
    Public Sub New(clID As String)
        clientID = clID
        clientSecret = ""
        myPin = ""
        imgToken = New Token
        LastImageLink = ""
        UseClipboard = True
        AnonOnly = True
    End Sub


    Private clientID As String
    Private clientSecret As String

    Private AnonOnly As Boolean = True

    ' tokens are not public
    Private imgToken As Token

    Public Property LastImageLink As String

    Public Property UseClipboard As Boolean

    ' precise moment when it expires for use in code
    Public ReadOnly Property TokenExpiry As DateTime
        Get
            If imgToken IsNot Nothing Then
                Return imgToken.Expiry
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Function GetExpiryCountdown() As String
        Return String.Format("{0:hh\:mm\:ss}", GetExpiryTimeRemaining)
    End Function

    ' time left as a TimeSpan
    Public Function GetExpiryTimeRemaining() As TimeSpan
        Dim ts As New TimeSpan(0)

        If imgToken Is Nothing Then
            Return ts
        End If

        If DateTime.Now > imgToken.Expiry Then
            Return ts
        Else
            ts = imgToken.Expiry - DateTime.Now
            Return ts
        End If

    End Function

    Public Function IsTokenValid() As Boolean

        If imgToken Is Nothing Then
            Return False
        End If

        If String.IsNullOrEmpty(imgToken.AcctUserName) Then
            Return False
        End If

        If imgToken.IsExpired Then
            Return False
        End If

        Return True

    End Function

    ' Currently, the PIN is set from a calling App.  Might be possible
    ' to feed the log in to imgUr to get a PIN
    Private myPin As String
    Public WriteOnly Property Pin As String
        Set(value As String)
            myPin = value
        End Set
    End Property


    ' Navigates to the web page.
    ' see wb_DocumentCompleted for code to 
    ' parse the PIN from the document
    Public Sub RequestPinBrowser(BrowserCtl As WebBrowser)

        If AnonOnly Then
            ' you do not need a PIN for Anon
            Throw New ApplicationException("A PIN is not needed for ANON Uploads")
            Exit Sub
        End If

        If BrowserCtl Is Nothing Then
            Throw New ArgumentException("Missing a valid WebBrowser reference")
            Exit Sub
        End If

        ' imgur API format
        ' https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=REQUESTED_RESPONSE_TYPE&state=APPLICATION_STATE

        Dim OAuthUrlTemplate = "https://api.imgur.com/oauth2/authorize?client_id={0}&response_type={1}&state={2}"
        Dim ReqURL As String = String.Format(OAuthUrlTemplate, clientID, "pin", "ziggy")

        BrowserCtl.Url = New Uri(ReqURL)
    End Sub


    Public Function GetAccessToken() As imgUrResults
        ' there are different types of token requests
        ' which vary only by the data submitted

        Dim sReq As String = String.Format("client_id={0}&client_secret={1}&grant_type=pin&pin={2}",
                                            clientID, clientSecret, myPin)
        If myPin = String.Empty Then
            Return imgUrResults.InvalidPIN
        End If

        If AnonOnly Then Return imgUrResults.InvalidRequest

        ' call generic token processor
        Return RequestToken(sReq)

    End Function

    ' request a Token 
    Private Function RequestToken(sRequest As String) As imgUrResults
        Dim url As String = "https://api.imgur.com/oauth2/token/"

        Dim myResult As imgUrResults = imgUrResults.OK

        ' create request for the URL, using POST method
        Dim request As WebRequest = WebRequest.Create(url)
        request.Method = "POST"

        ' convert the request, set content format, length
        Dim data As Byte() = System.Text.Encoding.UTF8.GetBytes(sRequest)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length

        ' write the date to request stream
        Dim dstream As Stream = request.GetRequestStream
        dstream.Write(data, 0, data.Length)
        dstream.Close()

        ' json used on the response and potential WebException
        Dim json As New JavaScriptSerializer()

        ' prepare for a response
        Dim response As WebResponse = Nothing
        Dim SvrResponses As Dictionary(Of String, Object)

        Try
            response = request.GetResponse
            ' convert status code to programmatic result
            myResult = GetResultFromStatus(CType(response, HttpWebResponse).StatusCode)

        Catch ex As WebException
            ' a bad/used pin will throw an exception
            Dim resp As String = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()

            SvrResponses = CType(json.DeserializeObject(resp.ToString), 
                                    Dictionary(Of String, Object))
            myResult = GetResultFromStatus(Convert.ToString(SvrResponses("status")))

        End Try

        'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        'Console.WriteLine(CType(response, HttpWebResponse).StatusCode)

        ' premature evacuation
        If myResult <> imgUrResults.OK Then
            If dstream IsNot Nothing Then
                dstream.Close()
                dstream.Dispose()
            End If
            If response IsNot Nothing Then
                response.Close()
            End If

            Return myResult
        End If

        ' read the response stream
        dstream = response.GetResponseStream
        Dim SvrResponseStr As String
        Using sr As StreamReader = New StreamReader(dstream)
            ' stream to string
            SvrResponseStr = sr.ReadToEnd
            'Console.WriteLine(SvrResponse)
        End Using

        ' close streams
        dstream.Close()
        dstream.Dispose()
        response.Close()

        Try
            ' use json serialier to parse the result(s)
            ' convert SvrRsponse to Dictionary
            SvrResponses = CType(json.DeserializeObject(SvrResponseStr), 
                                    Dictionary(Of String, Object))

            ' get stuff from Dictionary
            imgToken.AccessToken = Convert.ToString(SvrResponses("access_token"))
            imgToken.RefreshToken = Convert.ToString(SvrResponses("refresh_token"))
            imgToken.AcctUserName = Convert.ToString(SvrResponses("account_username"))

            ' convert expires_in to a point in time
            Dim nExp As Integer = Convert.ToInt32(Convert.ToString(SvrResponses("expires_in")))
            imgToken.Expiry = Date.Now.Add(New TimeSpan(0, 0, nExp))

            ' Pins are single use
            ' throw it away since it is no longer valid 
            myPin = ""

        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            myResult = imgUrResults.TokenParseError
        End Try

        Return myResult


    End Function

    ' public interface to check params before trying to upload
    Public Function UploadImage(filename As String, Optional Anon As Boolean = False) As imgUrResults

        If AnonOnly Then
            Return DoImageUpLoad(filename, AnonOnly)
        Else
            If IsTokenValid() = False Then
                Return imgUrResults.InvalidToken
            End If
        End If

        ' should be the job of the calling app to test for FileExist
        Return DoImageUpLoad(filename, Anon)

    End Function

    ' actual file uploader
    Private Function DoImageUpLoad(fileName As String, Optional Anon As Boolean = False) As imgUrResults
        Dim result As imgUrResults = imgUrResults.OK
        LastImageLink = ""

        Try
            ' create a WebClient 
            Using wc = New Net.WebClient()
                ' read image
                Dim values = New NameValueCollection() From
                        {
                            {"image", Convert.ToBase64String(File.ReadAllBytes(fileName))}
                        }
                ' type of headers depends on whether this is an ANON or ACCOUNT upload
                If Anon Then
                    wc.Headers.Add("Authorization", "Client-ID " + clientID)
                Else
                    wc.Headers.Add("Authorization", "Bearer " & imgToken.AccessToken)
                End If

                ' upload, get response
                Dim response = wc.UploadValues("https://api.imgur.com/3/upload.xml", values)

                ' read response converting byte array to stream
                Using sr As New StreamReader(New MemoryStream(response))
                    Dim uplStatus As String
                    Dim SvrResponse As String = sr.ReadToEnd

                    Dim xdoc As XDocument = XDocument.Parse(SvrResponse)
                    ' get the status of the request
                    uplStatus = xdoc.Root.Attribute("status").Value
                    result = GetResultFromStatus(uplStatus)

                    If result = imgUrResults.OK Then
                        LastImageLink = xdoc.Descendants("link").Value

                        ' only overwrite the server result status
                        If String.IsNullOrEmpty(LastImageLink) Then
                            ' avoid NRE elsewhere
                            LastImageLink = ""
                            ' we did something wrong parsing the result
                            ' but this one is kind of minor
                            result = imgUrResults.OtherAPIError
                        End If
                    End If

                End Using

                If UseClipboard AndAlso (result = imgUrResults.OK) Then
                    Clipboard.SetText(LastImageLink)
                End If

            End Using
        Catch ex As Exception
            Dim errMsg As String = ex.Message

            ' rate limit
            If ex.Message.Contains("429") Then
                result = imgUrResults.RateLimitError

                ' internal error
            ElseIf ex.Message.Contains("500") Then
                result = imgUrResults.ServerError

            End If
        End Try

        Return result
    End Function

    Private Function GetResultFromStatus(status As String) As imgUrResults

        Select Case status.Trim
            Case "200"
                Return imgUrResults.OK
            Case "400"
                Return imgUrResults.BadRequestFormat
            Case "401"
                Return imgUrResults.AuthorizationError
            Case "403"
                Return imgUrResults.Forbidden
            Case "404"
                Return imgUrResults.NotFound
            Case "429"
                Return imgUrResults.RateLimitError
            Case "500"
                Return imgUrResults.ServerError
            Case Else
                ' Stop - work out other returns
                Return imgUrResults.UknownStatus
        End Select
    End Function

    Private Function GetResultFromStatus(status As Int32) As imgUrResults
        ' some places we get a string, others an integer
        Return GetResultFromStatus(status.ToString)
    End Function

End Class

如何使用

该过程需要Web浏览器供用户导航和请求PIN。对于测试/开发,我使用了一个WebBrowser控件并从返回的页面中获取了PIN。

注意:为了进行测试,我的imgUR帐户设置为DESKTOP,因为我们是从DESKTOP应用程序发送的。此外,这是您将图像发送到您的帐户。其他人没有办法上传到您的帐户,而不会在应用程序中提供您的秘密ID和/或嵌入您的主ImgUR登录名和密码。这就是ImgUR设计它的方式。

:一种。创建一个imgUR对象:

Friend imgUR As imgurAPI
imgUR = New imgurAPI(<your Client ID>,<your secret code>)

<强> B中。获得一个Pin - 方法一

' pass the app's WebBrowser Control
imgUR.RequestPinBrowser(wb)

这会将您带到一个imgur页面,您必须在该页面上授权发送PIN才能上传到您的帐户。输入您的帐户名称,密码,单击“允许”。将显示带有PIN的新页面。将PIN从网页复制到其他控件,可以将其提供给imgurAPI类。

下面的代码可以解析PIN页面并将其转换为其他控件。

方法二

  • 使用您自己的外部浏览器,转到

https://api.imgur.com/oauth2/authorize? client_id=YOUR_CLIENT_ID&response_type=pin&state=ziggy

  • 登录
  • 将您收到的PIN码复制到TextBox或其他内容,以便将其发送到imgurAPI:
  • 设置图钉:imgUR.Pin = <<PIN YOU RECEIVED>>

无论你是否想要在表单中包含WebBrowser控件,这个过程都是相同的。 PIN只能在短时间内使用,因此您必须立即使用它来获取访问令牌。

<强>℃。获取访问令牌

' imgUrResults is an enum exposed by the class
Dim result As imgurAPI.imgUrResults = imgUR.RequestToken

注意:

  • imgUR类将保留令牌
  • 令牌目前在1小时(3600秒)到期

D:上传文件
使用imgUR.UploadImage(filename, boolAnon)

上传

文件名 - 要上传的文件

boolAnon - 布尔标志。 False =将此文件上传到您的帐户,而不是Anon常规池方法。

示例:

' get token
Dim result As imgurAPI.imgUrResults = imgUR.RequestToken

' check result
If result = imgurAPI.imgUrResults.OK Then
    ' assumes the file exists
    imgUR.UploadImage("C:\Temp\London.jpg", False)
Else
    MessageBox.Show(String.Format("Error getting access token. Status:{0}",
        result.ToString))
End If

文件上传后,该过程会在响应中查找链接。如果可以解析链接,它将从LastImageLink属性中获取并粘贴到ClipBoard。

杂项属性,设置和方法

LastImageLink (字符串) - 上传的最后一张图片的网址

UseClipBoard (Bool) - 如果为true,则imgurAPI类将上传图像的链接发布到剪贴板

TokenExpiry (日期) - 当前令牌过期的日期时间

GetTokenTimeRemaining ()As TimeSpan - 表示当前令牌到期前多长时间的TimeSpan

公共函数GetTokenCountdown() As String - TimeRemaining的格式化字符串

Public WriteOnly Property Pin As String - 获取访问令牌所需的PIN

Public Function IsTokenValid() As Boolean - 当前令牌有效

Public Function IsTokenExpired ()As Boolean - TimeRemaining与DateTime.Now的简单布尔版本

备注

  • 可以续订或扩展令牌。但由于它们持续了一个小时,这似乎很多。
  • PINS只能在短时间内使用。一旦PIN被交换为令牌,imgurAPI(此类)将清除PIN。如果获取令牌时出现问题,您必须先获得一个新的PIN(或者如果您在几分钟前得到它就粘贴最后一个)。
  • 除非/直到您更改帐户设置,否则整个世界都无法看到上传的图片。
  • 您可以重设您的SecretID(设置 - &gt;应用程序)。如果这样做,您还需要为使​​用此API类的应用重置它,并重新编译(或从配置文件中读取它)。

如果您使用WebBrowser控件来获取PIN码,则可以将此代码添加到DocumentCompleted事件中,以便从HTML中抓取PIN码:

' wb is the control
Dim htmlDoc As System.Windows.Forms.HtmlDocument = wb.Document
Dim elP As System.Windows.Forms.HtmlElement = htmlDoc.GetElementById("pin")

If elP IsNot Nothing Then
    sPin = elP.GetAttribute("value")
    If String.IsNullOrEmpty(sPin) = False Then
       ' user has to push the button for `imgUR.Pin = tbPIN.Text`
       ' this is in case the HTML changes, the user can override
       ' and input the correct PIN
       Me.tbPIN.Text = sPin
    End If

End If

关于OAuth模型

这是非正式的 - 从阅读文档和使用API​​学到的信息。截至此日期适用于imgur API v3。

没有自动获取PIN码。无论如何,您必须导航到浏览器中的URL并输入您的帐户名和密码才能获得PIN。这是设计使您自己亲自授权某些外部应用访问您的帐户内容。

上面的

方法一使用.NET WebBrowser控件来执行此操作。使用此方法,我们可以确保您和imgur类都使用相同的Endpoint / URL,因为它通过浏览器控件将您发送到那里。

方法二只是你在某个浏览器,任何浏览器中去那里。登录,获取PIN,然后将其粘贴到imgurAPI类。

无论方法如何,使用的正确端点/ URL是:

https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=pin&state=foobar

使用imgurAPI类在表单上使用浏览器,显然我们可以确定您和类都使用相同的URL和ClientID。 DocumentComplete的代码将PIN提取到TextBox ,您仍然需要在课程中设置它:

myimgUR.PIN = tbPinCode.Text

PINS是一次性使用,并且过期。

所以特别是在开发时,你停止代码,添加一些东西然后自然重新运行,代码将不再有旧的令牌或PIN。如果上一个PIN是最近的并且提交,您可能不需要新的,但我发现很难记住是否是这种情况。

该课程将PINS视为一次性使用。收到令牌后,它会清除变量,因为它们已被使用且不再有效。


最终修改

  • 添加了仅限Anon 模式

要使用该类仅以匿名模式(一般网站,而不是您的帐户)上传,则不需要SecretID。为此,请使用新的构造函数overload:

Public Sub New(clientID As String)

这会将类设置为仅使用Anon,并且某些方法在使用基于帐户的方法(例如GetToken)时将返回错误或引发异常。如果仅使用ClientID初始化它,它将保持 AnonOnly 模式,直到您使用ClientID和SecretID重新创建对象。

没有任何理由将其用作AnonOnly(除非您没有帐户),因为UploadImage方法允许您将其指定为按文件匿名上传:

Function UploadImage(filename As String, 
                     Optional Anon As Boolean = False) As imgUrResults
  • 修订/澄清了imgUrResults Enum

这意味着无所不包:一些返回表示类检测到的问题,另一些返回是服务器响应,只是简单地传递。

  • 已移除IsTokenExpired

IsTokenValid更彻底。还有其他方法可以获得剩余时间或实际到期时间。

  • 添加了各种错误捕获/处理
    • 请求PIN时检查有效的WebBrowser控件
    • 优化了上传图片后用于获取服务器状态码的方法
    • 重写了一些处理以优先考虑远程服务器状态而不是类返回