flickr.UploadPicture returns null

Topics: Developer Forum
Apr 17, 2008 at 1:37 PM
I have created the following upload function on an ASP.NET page, and when uploading the photo it returns Nothing. No error occurs but the file does not get uploaded.

    Public Sub UploadFile()
        Dim flickr As New FlickrNet.Flickr()
        flickr.ApiKey = ConfigurationManager.AppSettings("FlickrAPIKey")
        flickr.ApiSecret = ConfigurationManager.AppSettings("FlickrSecret")
 
        Dim title As String = "Test Photo"
        Dim description As String = "This is the description of the photo"
        Dim tags As String = "tag1,tag2,tag3"
        Dim photoId As String = flickr.UploadPicture(FileUpload1.PostedFile.InputStream, title, description, tags, 1, 1, 1)
    End Sub 

I don't think this is a authentication problem as the "recent photos" function shown below works fine and uses the same key/secret:

    Public Shared Function RecentPhotos() As FlickrNet.PhotoCollection
        Dim flickr As New FlickrNet.Flickr()
        flickr.ApiKey = ConfigurationManager.AppSettings("FlickrAPIKey")
        flickr.ApiSecret = ConfigurationManager.AppSettings("FlickrSecret")
 
        Dim options As New FlickrNet.PhotoSearchOptions()
        options.UserId = ConfigurationManager.AppSettings.Get("UserId")
        options.PerPage = 20
        options.Page = 1
        options.SortOrder = FlickrNet.PhotoSearchSortOrder.DatePostedDesc
 
        Dim photos As FlickrNet.Photos = flickr.PhotosSearch(options)
        If photos.PhotoCollection Is Nothing Then
            Return New FlickrNet.PhotoCollection()
        Else
            Return photos.PhotoCollection
        End If
    End Function 

Does anyone have any thoughts as to what the issue may be?

Thanks,
Mark
Coordinator
Apr 17, 2008 at 2:40 PM
The Flickr Upload method requires full authentication - i.e. not just a key and secret, but an authentication token as well.

This should be throwing an exception - not returning an empty string.
Apr 19, 2008 at 4:05 PM

samjudson wrote:
The Flickr Upload method requires full authentication - i.e. not just a key and secret, but an authentication token as well.

This should be throwing an exception - not returning an empty string.

Thanks for your response Sam, I've tried adding a call to authenticate myself (which is working) but I still face the same issue. For example, using a button on Page1, I make a call with the following code:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim flickr As New FlickrNet.Flickr()
        flickr.ApiKey = ConfigurationManager.AppSettings("FlickrAPIKey")
        flickr.ApiSecret = ConfigurationManager.AppSettings("FlickrSecret")
 
        ' Want to request "read" access so call the AuthCalcWebUrl method.
        Dim auth_url As String = flickr.AuthCalcWebUrl(FlickrNet.AuthLevel.Write)
 
        ' Redirect to that URL
        Response.Redirect(auth_url)
 
    End Sub
This correctly authenticates me and via the Callback URL it redirects me to Page2 which has the following code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
            If Request.QueryString("frob") Is Nothing Then
                Label1.Text = "Error: Frob not found in URL"
                Label1.ForeColor = Drawing.Color.Red
                Return
            End If
 
            Dim frob As String = Request.QueryString("frob")
 
            Dim flickr As New FlickrNet.Flickr()
            Dim auth As FlickrNet.Auth = flickr.AuthGetToken(frob)
 
            Session("AUTH_TOKEN") = auth.Token
            Session("USER_ID") = auth.User.UserId
            Session("USER_NAME") = auth.User.Username
 
            Label1.Text = Session("USER_NAME") & " successfully authenticated."
        End If
    End Sub
 
    Public Sub UploadFile()
        Dim flickr As New FlickrNet.Flickr()
        flickr.ApiKey = ConfigurationManager.AppSettings("FlickrAPIKey")
        flickr.ApiSecret = ConfigurationManager.AppSettings("FlickrSecret")
        flickr.AuthToken = Session("AUTH_TOKEN")
 
        Dim title As String = "Test Photo"
        Dim descripton As String = "This is the description of the photo"
        Dim tags As String = "tag1,tag2,tag3"
        Dim photoId As String = flickr.UploadPicture("C:\Program Files\Microsoft Office\Templates\Presentation Designs\Maple.gif", title, descripton, tags, 1, 1, 1)
    End Sub
 
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        UploadFile()
    End Sub
When I run through the Page Load event, the "flickr.AuthGetToken" gets the token correctly and stores it in a session variable (and the page also reports that I was "successfully authenticated"). When I click the upload button, I set the "AuthToken" to be the value in the session variable (which is the token I previously received) but the "UploadPicture" method still returns an empty string (or Nothing/null depending on how you want to look at it).

Any more ideas?

Thanks,
Mark
Coordinator
Apr 19, 2008 at 4:39 PM
No ideas yet - I'll do some testing at my end.

I take it the file you are specifying is on the server, and not on the client PC?
Apr 20, 2008 at 4:57 PM
Yes, that's correct - the file is on the web server.

Thanks,
Mark