none
Making post on pastebin using their API in Visual Basic

    Question

  • I came across some code I would love to use in my notepad project I'm currently working on. Can someone help me understand some of it tho? I'm having a hard time understanding where you enter the text that it posted. I'm looking to make a button post it and the text body to be in a rich text box and the title to be in a text box.  Here's the code: 

    Imports System.Collections.Specialized
    Imports System.Net
    Imports System.Text
    
    Public Class Form1
        Private Const login_url As String = "http://pastebin.com/api/api_login.php"
        Private Const post_url As String = "http://pastebin.com/api/api_post.php"
        Private Const dev_key As String = "32e06c2da46f91db964deb1a33db8ab0"
        Private Const user_name As String = "??????"
        Private Const user_password As String = "???????"
        Private user_key As String = ""
    
        Public Sub CallIt()
            'Login to generate the user_key
            If Login() Then
                ' Then post a new paste
                Dim PasteURL As String = Post("Code", "Name", "vb")
    
            End If
        End Sub
    
        Public Function Login() As Boolean
    
            Dim Data As New NameValueCollection()
            Data.Add("api_dev_key", dev_key)
            Data.Add("api_user_name", user_name)
            Data.Add("api_user_password", user_password)
    
            Dim Status As Boolean = True
            Using Client As New WebClient()
                Dim Response As String = Encoding.UTF8.GetString(Client.UploadValues(login_url, Data))
    
                If Response.Contains("Bad API request") Then
                    Status = False
                Else
                    user_key = Response
                End If
    
            End Using
    
            Return Status
        End Function
    
        Public Function Post(ByVal paste_code As String,
                             ByVal paste_name As String,
                             ByVal paste_format As String) As String
            If user_key = "" Then Return False
    
            Dim Data As New NameValueCollection()
            Data.Add("api_dev_key", dev_key)
            Data.Add("api_user_key", user_key)
            Data.Add("api_option", "paste")
            Data.Add("api_paste_code", paste_code)
            Data.Add("api_paste_name", paste_name)
            Data.Add("api_paste_format", paste_format)
            Data.Add("api_paste_private", "2")  '0=public 1=unlisted 2=private
            Data.Add("api_paste_expire_date", "N")
    
            Dim PasteURL As String = ""
            Using Client As New WebClient()
                Dim Response As String = Encoding.UTF8.GetString(Client.UploadValues(post_url, Data))
    
                If Not Response.Contains("Bad API request") Then
                    PasteURL = Response
                End If
            End Using
    
            Return PasteURL
        End Function
    End Class
    

    Saturday, April 1, 2017 9:29 PM

Answers

  • So i got that all and I've been looking at there api help (https://pastebin.com/api). I don't understand how to make the button post it tho. Also thanks for all the help so far.

    Simple example, returns either "Error" or the URL

    Imports System.Net
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
        Public Function NewPaste(ByVal Content As String) As String
            
            Dim api_dev_key As String = "" '<-- <<<<< Your API key here >>>>>
            
            Dim api_paste_code As String = URLEncode(Content)
            Dim api_paste_private As String = "2"
            Dim api_paste_expire_date As String = "10M"
            Dim api_paste_format As String = "php"
            Dim api_user_key As String = ""
            Dim Response As String = HttpPost("http://pastebin.com/api/api_post.php", "api_option=paste&api_dev_key=" & api_dev_key & "&api_paste_code=" & api_paste_code)
            If Response.Contains("Bad API request") = False Then
                Return Raw(Response)
            Else
                Return "Error"
            End If
        End Function
        Private Function URLEncode(ByVal EncodeStr As String) As String
            Dim i As Integer
            Dim erg As String
            erg = EncodeStr
            erg = Replace(erg, "%", Chr(1))
            erg = Replace(erg, "+", Chr(2))
            For i = 0 To 255
                Select Case i
                    Case 37, 43, 48 To 57, 65 To 90, 97 To 122
                    Case 1
                        erg = Replace(erg, Chr(i), "%25")
                    Case 2
                        erg = Replace(erg, Chr(i), "%2B")
                    Case 32
                        erg = Replace(erg, Chr(i), "+")
                    Case 3 To 15
                        erg = Replace(erg, Chr(i), "%0" & Hex(i))
                    Case Else
                        erg = Replace(erg, Chr(i), "%" & Hex(i))
                End Select
            Next
            Return erg
        End Function
        Public Function Raw(ByVal URL As String)
            Dim ID As String = URL.Substring(URL.LastIndexOf("/") + 1)
            ID = "http://pastebin.com/raw.php?i=" & ID
            Return ID
        End Function
        Private Function HttpPost(ByVal URL As String, ByVal Data As String)
            Dim request As WebRequest = WebRequest.Create(URL)
            request.Method = "POST"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim response As WebResponse = request.GetResponse()
            'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()
            Return responseFromServer
        End Function
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = NewPaste("This is only a test")
        End Sub
    End Class
    

    verified as working by me....

    .NET has a built-in URLEncode also.

    • Edited by Devon_Nullman Monday, April 3, 2017 3:00 AM Add info
    • Marked as answer by TimeFluid Monday, April 3, 2017 5:20 PM
    Monday, April 3, 2017 2:43 AM

All replies

  • I'm having a hard time understanding where you enter the text that it posted.

    Assuming that "vb" is a literal to indicate the formatting, then the code would be something like:

        Dim PasteURL As String = Post(RichTextBox1.Text, TextBox1.text, "vb")

    Your button click event handler would be

        CallIt

    If you want to retrieve the pasted URL then you need to declare PasteURL at the form level.  Or, make CallIt a function that returns PasteURL.


    • Edited by AcamarMVP Saturday, April 1, 2017 10:51 PM sp
    Saturday, April 1, 2017 10:38 PM
  • So i got that all and I've been looking at there api help (https://pastebin.com/api). I don't understand how to make the button post it tho. Also thanks for all the help so far.
    Sunday, April 2, 2017 9:33 PM
  • So i got that all and I've been looking at there api help (https://pastebin.com/api). I don't understand how to make the button post it tho. Also thanks for all the help so far.
    What was the result when you implemented the code I suggested?
    Sunday, April 2, 2017 11:14 PM
  • So i got that all and I've been looking at there api help (https://pastebin.com/api). I don't understand how to make the button post it tho. Also thanks for all the help so far.

    Simple example, returns either "Error" or the URL

    Imports System.Net
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
        Public Function NewPaste(ByVal Content As String) As String
            
            Dim api_dev_key As String = "" '<-- <<<<< Your API key here >>>>>
            
            Dim api_paste_code As String = URLEncode(Content)
            Dim api_paste_private As String = "2"
            Dim api_paste_expire_date As String = "10M"
            Dim api_paste_format As String = "php"
            Dim api_user_key As String = ""
            Dim Response As String = HttpPost("http://pastebin.com/api/api_post.php", "api_option=paste&api_dev_key=" & api_dev_key & "&api_paste_code=" & api_paste_code)
            If Response.Contains("Bad API request") = False Then
                Return Raw(Response)
            Else
                Return "Error"
            End If
        End Function
        Private Function URLEncode(ByVal EncodeStr As String) As String
            Dim i As Integer
            Dim erg As String
            erg = EncodeStr
            erg = Replace(erg, "%", Chr(1))
            erg = Replace(erg, "+", Chr(2))
            For i = 0 To 255
                Select Case i
                    Case 37, 43, 48 To 57, 65 To 90, 97 To 122
                    Case 1
                        erg = Replace(erg, Chr(i), "%25")
                    Case 2
                        erg = Replace(erg, Chr(i), "%2B")
                    Case 32
                        erg = Replace(erg, Chr(i), "+")
                    Case 3 To 15
                        erg = Replace(erg, Chr(i), "%0" & Hex(i))
                    Case Else
                        erg = Replace(erg, Chr(i), "%" & Hex(i))
                End Select
            Next
            Return erg
        End Function
        Public Function Raw(ByVal URL As String)
            Dim ID As String = URL.Substring(URL.LastIndexOf("/") + 1)
            ID = "http://pastebin.com/raw.php?i=" & ID
            Return ID
        End Function
        Private Function HttpPost(ByVal URL As String, ByVal Data As String)
            Dim request As WebRequest = WebRequest.Create(URL)
            request.Method = "POST"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim response As WebResponse = request.GetResponse()
            'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()
            Return responseFromServer
        End Function
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = NewPaste("This is only a test")
        End Sub
    End Class
    

    verified as working by me....

    .NET has a built-in URLEncode also.

    • Edited by Devon_Nullman Monday, April 3, 2017 3:00 AM Add info
    • Marked as answer by TimeFluid Monday, April 3, 2017 5:20 PM
    Monday, April 3, 2017 2:43 AM
  • This code works great besides it's not using my account. I need it to post on my Pastebin account.
    Monday, April 3, 2017 3:22 AM
  • I just couldn't find out how to make it post.
    Monday, April 3, 2017 3:22 AM
  • This code works great besides it's not using my account. I need it to post on my Pastebin account.

    If you put your api dev key it should post to your account. Is that not happening ? Or are you replying to someone else?

    Monday, April 3, 2017 3:56 AM
  • I was talking about the other guys. I love your code @Devon, but I'm a bit lost on how to make your code post with a button click.
    Monday, April 3, 2017 5:14 AM
  • Oh no. I just got confused on who was who. So your code works but it's not posting under my account. Is it not from https://pastebin.com/api/api_user_key.html?
    Monday, April 3, 2017 5:36 AM
  • Oh no. I just got confused on who was who. So your code works but it's not posting under my account. Is it not from https://pastebin.com/api/api_user_key.html?
    If you have solved your problem of understanding where you enter the text to be posted, and how to make a button post it, then you should mark replies that were helpful as answers, and post a new question about your next problem, with the code you are now using.  Otherwise it is too difficult to work out what your question is.
    Monday, April 3, 2017 6:04 AM