I create a code for upload a image in picasa: sorry for my bad english :)

   - First imports library:

Imports System.IO
Imports System.Net
Imports System.Text
Imports Newtonsoft.Json
Imports System.Text.RegularExpressions


   - Now code:

    'Your Data this found in OAuth: https://console.developers.google.com/ 
    Dim CLIENT_ID As String = "xxxxxxx.apps.googleusercontent.com"
    Dim CLIENT_SECRET As String = "V_xxxxxx"
    Dim SCOPE As String = "http://picasaweb.google.com/data/";
    Dim REDIRECT_URI As String = "urn:xxxxx"

'Class for DeserializeObject json results:
    Public Class Token
        Public Property access_token As String
        Public Property token_type As String
        Public Property expires_in As Integer
        Public Property refresh_token As String
    End Class


   - For example in a button add this code for get authorization code:

 'First step: get authorization_code, after get the code it is not 
necessary to make this step again.

        Dim authorization_code As String = 
String.Format("https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={0}&redirect_uri={1}&scope={2}&access_type=offline&approval_prompt=auto";,
                                                       CLIENT_ID, 
REDIRECT_URI, SCOPE)
        Process.Start(authorization_code)


        Dim authCode As String = InputBox("Authorisation code", 
"Authorisation Code", "")


        'Second step: Get of token and save in any place :P
        Dim postdata As New ArrayList
        postdata.Clear()
        postdata.Add("code=" + authCode)
        postdata.Add("client_id=" + CLIENT_ID)
        postdata.Add("client_secret=" + CLIENT_SECRET)
        postdata.Add("redirect_uri=" + REDIRECT_URI)
        postdata.Add("grant_type=authorization_code")


        Dim data As String = String.Join("&", postdata.ToArray())
        Dim request As HttpWebRequest = HttpWebRequest.Create(
"https://www.googleapis.com/oauth2/v3/token";)
        Dim byteData() As Byte = Encoding.UTF8.GetBytes(data)


        request.Host = "www.googleapis.com"
        request.Method = WebRequestMethods.Http.Post
        request.ProtocolVersion = HttpVersion.Version11
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteData.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteData, 0, byteData.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = request.GetResponse()
        Dim reader As Stream = response.GetResponseStream()
        Dim responseStreamReader As StreamReader = New StreamReader(reader)


        'GET response Json in a string or textbox or textfile or word file 
:P
        TextBox1.Text = responseStreamReader.ReadToEnd
        response.Close()        
        'save the result and not use First step and second again! 

   - Functions main:

 'Function for DeserializeObject the previous result!
    Private Function deserialize(ByVal json As String, ByVal valor As 
String)
        Dim a As Token = JsonConvert.DeserializeObject(Of Token)(json)
        Dim resultado As String = String.Empty
        If valor = "token" Then
            valor = a.access_token
            Return valor
            Exit Function
        ElseIf valor = "refresh" Then
            valor = a.refresh_token
            Return valor
            Exit Function
        ElseIf valor = "expire" Then
            valor = a.expires_in
            Return valor
            Exit Function
        End If
        Return Nothing
    End Function

 'Function for refresh of token the previous result!
    Private Function refresh_token(ByVal refresh__token As String) As String
        Dim postdata As New ArrayList
        postdata.Clear()
        postdata.Add("client_id=" + CLIENT_ID)
        postdata.Add("client_secret=" + CLIENT_SECRET)
        postdata.Add("refresh_token=" + refresh__token)
        postdata.Add("grant_type=refresh_token")


        Dim data As String = String.Join("&", postdata.ToArray())
        Dim request As HttpWebRequest = 
HttpWebRequest.Create("https://www.googleapis.com/oauth2/v3/token";)
        Dim byteData() As Byte = Encoding.UTF8.GetBytes(data)


        request.Host = "www.googleapis.com"
        request.Method = WebRequestMethods.Http.Post
        request.ProtocolVersion = HttpVersion.Version11
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteData.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteData, 0, byteData.Length)
        dataStream.Close()
        Dim valor As String
        Dim response As HttpWebResponse = request.GetResponse()
        Dim reader As Stream = response.GetResponseStream()
        Dim responseStreamReader As StreamReader = New StreamReader(reader)
        valor = responseStreamReader.ReadToEnd
        response.Close()
        reader.Close()
        Return valor
    End Function

'Function for upload post image in picasa!
    Public Shared Function PostImage(ByVal token As String)
        Dim valor As String = String.Empty
        Dim databyte As Byte() = File.ReadAllBytes("C:\Users\1.jpg") 'Your 
patch of your image
        'USER ID, ALBUMID AND YOUR TOKEN. 
        'Go to you album, and copy link RSS right menu in notepad, and copy 
here USER ID and ALBUMID, this is numeric values!!
        Dim url As String = String.Format(
"https://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}?access_token={2}";
, "USER-ID-HERE", "ALBUM-ID-HERE", token)
        Dim request As HttpWebRequest = CType(WebRequest.Create(url), 
HttpWebRequest)
        request.Headers.Add("GData-Version", "2")
        request.ContentType = "image/jpeg"
        request.ContentLength = databyte.Length
        'Your name of your image:
        request.Headers.Add("Slug", "NAME-IMAGE-HERE.jpg")
        request.Method = WebRequestMethods.Http.Post


        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(databyte, 0, databyte.Length)
        dataStream.Close()


        Dim response As HttpWebResponse = request.GetResponse()
        Dim reader As Stream = response.GetResponseStream()
        Dim responseStreamReader As StreamReader = New StreamReader(reader)
        valor = responseStreamReader.ReadToEnd
        response.Close()
        reader.Close()
        Return valor
    End Function


   - USAGE: for example in a new button:

 'USAGE: ------------>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button2.Click
        'Remember, I save the first result Json in textbox1!
        'Now i upload my image, my first token dead, for this is necessary 
refresh
        Dim refresh As String = refresh_token(deserialize(TextBox1.Text, 
"refresh"))

        'DeserializeObject the previous result, whith contains new token!
        TextBox2.Text = PostImage(deserialize(refresh, "token"))


        'Now result save in textbox2, get of url link direct, with regultar 
expresion
        Dim r As New System.Text.RegularExpressions.Regex("<content type='
image/jpeg' src='(.*?)'")
        Dim matches As MatchCollection = r.Matches(TextBox2.Text)
        For Each itemcode As Match In matches
            'Get of url link direct
            MsgBox(itemcode.Groups(1).ToString().Trim())
        Next

        'This working 100% :P 
    End Sub

if you can improve my code making it more efficient, share it with everyone.

Greetings

-- 
You received this message because you are subscribed to the Google Groups 
"Google Picasa Web Albums API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-picasa-data-api.
For more options, visit https://groups.google.com/d/optout.

Reply via email to