Reusing a token.

Topics: Developer Forum
Nov 15, 2009 at 10:32 PM

I used the code from Coding4Fun article. Some part of the code has changed owing to changes made to the library.

What I would like to do is save the frob & the token to my registry. I have that part of the code ready.

How to use the saved frob & token from the registry to authenticate my application? I don't want the user to visit flickr url every time if the desktop has been authenticated.

 

Any ideas?

 

 

Imports FlickrNet

' Store the Frob in a private variable
Private tempFrob As String
Private ApiKey As String = "1234abcd1234abcd1234"
Private SharedSecret As String = "abcd1234abcd"

Private Sub AuthMeButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    ' Create Flickr instance
    Dim f As Flickr = New Flickr(ApiKey, SharedSecret)
    ' Get Frob    
    tempFrob = f.AuthGetFrob()
    ' Calculate the URL at Flickr to redirect the user to
    Dim flickrUrl As String = f.AuthCalcUrl(tempFrob, AuthLevel.Write)
    ' The following line will load the URL in the users default browser.
    System.Diagnostics.Process.Start(flickrUrl)

End Sub

Private Sub CompleteAuthButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    ' Create Flickr instance
    Dim f As Flickr = New Flickr(ApiKey, SharedSecret)    
    Try
        ' use the temporary Frob to get the authentication
        Auth a = f.AuthGetToken(tempFrob)
        ' Store this Token for later usage, 
        ' or set your Flickr instance to use it.
        Console.WriteLine("User authenticated successfully")
        Console.WriteLine("Authentication token is " & a.Token)
        f.AuthToken = a.Token
Console.WriteLine("User id is " & a.UserId)
    
    Catch ex As FlickrException

        ' If user did not authenticat your application 
        ' then a FlickrException will be thrown.
        Console.WriteLine("User did not authenticate you")
        Console.WriteLine(ex.ToString())
    End Try
End Sub

Coordinator
Nov 16, 2009 at 6:24 AM

You cannot and should not store and reuse the frob - that wont work.

You should keep and store the token however, then simply create your next instance of the Flickr object using it:

Dim f As New Flickr(ApiKey, SharedSecret, AuthToken)
Sam

 

 

Nov 16, 2009 at 12:30 PM

Thank you Sam.

I will attempt it this way and get back to you.

 

 

Nov 16, 2009 at 1:16 PM

Hi Sam,

Here's my code. I have put all this in a single button click.

Private Sub cmdAuthMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAuthMe.Click
        Try
            aTokenStr = GetSetting("FlickrWhoIs", "Authentication", "AuthToken")
            If Len(aTokenStr) = 0 Then
                ' Get Frob    
                tempFrob = f.AuthGetFrob()
                ' Calculate the URL at Flickr to redirect the user to
                Dim flickrUrl As String = f.AuthCalcUrl(tempFrob, AuthLevel.Write)
                ' The following line will load the URL in the users default browser.
                System.Diagnostics.Process.Start(flickrUrl)

'I WOULD LIKE THE APP TO WAIT TILL THE USER HAS COMPLETED AUTHORIZATION

a = f.AuthGetToken(tempFrob) aTokenStr = a.Token SaveSetting("FlickrWhoIs", "Authentication", "AuthToken", aTokenStr) Else f = New Flickr(apikey, secret, aTokenStr) MsgBox(f.IsAuthenticated, MsgBoxStyle.Information) End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
How would I make my desktop application wait till the user has completed clicking on I'll Authorize link in Flickr?

Coordinator
Nov 16, 2009 at 1:33 PM

You have no way of knowing that the user has authenticated, hence you must ask for some form of user response. This can either be a simple MessageBox("Click OK to continue") or splitting the code into multiple buttons as per your previous example.