- Interfaces
- Getting Authorized
- Example: Archivr
- Searching
- Uploading
- Conclusion
Example: Archivr
To show how the authorization process works, along with searching and uploading, we'll create an application that logs a user into Flickr, and, with the click of a button, downloads all the pictures in the user's account to a specified directory. Well call our app Archivr, as it'll give you a one-click archive of every file you've uploaded.
Since the user needs to log in before anything else can happen, we'll first bring up a modal dialog box that will either allow the user to log into the Flickr web page or cancel (see Figure 1).
Figure 1 Login screen for Flickr Archivr.
Clicking the Begin Login button brings up the Flickr web page in the system's default browser. This is where the user will be directed to log in and authorize this application. To do this, we need to get an instance of Flickr, determine the login URL, and then pass that on to a browser:
private void btnLogin_Click(object sender, System.EventArgs e) { string url = ""; flickr = new Flickr("your API key", "your shared secret"); frob = flickr.AuthGetFrob(); url = flickr.AuthCalcUrl(frob, AuthLevel.Write); btnLogin.Enabled = false; btnOkLogin.Enabled = true; System.Diagnostics.Process.Start(url); btnOkLogin.Enabled = true; }
Handling the login requires an instance of Frob, a string that will be used to retrieve your Token, which is what will uniquely identify this combination of logged-in user and your application. (Don't worry too much about what Frob stands for, because nobody on the Flickr API team seems to care to tell.) Take note that the AuthCalcUrl takes not only the Frob but also an indication of what level of permissions your app is requesting. This will be shown to the user when giving the user the option of granting or denying permissions. You can specify Read only, Write (which includes Read), Delete, or simply None.
Once the browser is open, the OK button can be enabled. All this button does is close the dialog box:
private void btnOkLogin_Click(object sender, System.EventArgs e) { this.Close(); }
The final authorization will be handled back in the main Form once the login dialog box has been closed. This is what the Load() function looks like for the main form:
private void Form1_Load(object sender, System.EventArgs e) { FormLogin loginDialog = new FormLogin(); Auth auth = null; this.Show(); loginDialog.ShowDialog(this); if(loginDialog.getFlickr() == null) { this.Dispose(); } else { frob = loginDialog.getFrob(); flickr = loginDialog.getFlickr(); auth = flickr.AuthGetToken(frob); token = auth.Token; user = auth.User; flickr = new Flickr(flickr.ApiKey, flickr.ApiSecret, token); loginDialog.Dispose(); } }
If the instance of Flickr is null, either because the user canceled or the authorization was not completed successfully, the application exits. Otherwise, the Flickr instance is retrieved, but is used only temporarily. In order to gain access to the non-public photos of the user who just logged in, a new instance of Flickr must be created that uses the token that was just retrieved. If the Flickr instance created without the token is used, only public photos would be returned in the search—not the user's private photos.
The result of all this effort is a global instance of Flickr that's authorized to access the private photos of the specified User, which is also stored globally for access elsewhere.