Hi,

Here's the full example of a method which uploads an image into an existing 
album in Picasa Web Albums. Feel free to share it!

Cheers,
Gizmo

public static String upload(String fullpathToImage)
    {        
      //The url of the image
        String resultingURL = new String();
        
        try
            {
       
            // These are not needed
            String GOOGLE_APP_NAME = "TralhasVariasScanPoster";
      
            String GOOGLE_REFRESH_TOKEN = "";
            String GOOGLE_ACCESS_TOKEN = "";
            
            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            TokenResponse tokenResponse = new TokenResponse();

            //Check of we have a previous Refresh Token cached
            if (Config.GOOGLE_OAUTH_REFRESH_TOKEN.length() == 0)
                {
                //No Google OAuth2 Key has been previously cached
            
                //Request the user to grant access to the Picasa Resource 
(uses the Google Authentication servers)
                AuthorizationCodeFlow.Builder codeFlowBuilder = 
                                new 
GoogleAuthorizationCodeFlow.Builder(httpTransport, 
                                        jsonFactory, 
                                        Config.GOOGLE_CLIENT_ID, 
                                        Config.GOOGLE_CLIENT_SECRET, 
                                        
Arrays.asList(Config.GOOGLE_SCOPE_PICASA));
                
                AuthorizationCodeFlow codeFlow = codeFlowBuilder.build();
                AuthorizationCodeRequestUrl authorizationUrl = 
codeFlow.newAuthorizationUrl();
                authorizationUrl.setRedirectUri(Config.GOOGLE_REDIRECT_URI);
                
                System.out.println("Go to the following address:\n" + 
authorizationUrl);
                System.out.println("What is the 'code' url parameter?");
                String code = new Scanner(System.in).nextLine();
                
                //Use the code returned by the Google Authentication Server 
to generate an Access Code
                AuthorizationCodeTokenRequest tokenRequest = 
codeFlow.newTokenRequest(code);
                tokenRequest.setRedirectUri(Config.GOOGLE_REDIRECT_URI);
                tokenResponse = tokenRequest.execute();
                
                GOOGLE_REFRESH_TOKEN = tokenResponse.getRefreshToken();
                GOOGLE_ACCESS_TOKEN = tokenResponse.getAccessToken();
                
                //Store the Refresh Token for later usage (this avoid 
having to request the user to 
                //Grant access to the application via the webbrowser again
                saveTextFile(Config.GOOGLE_OAUTH_REFRESH_TOKEN_FILE, 
GOOGLE_REFRESH_TOKEN);
                }
            else
                {
                //There is a Google OAuth2 Key cached previously.
                //Use the refresh token to get a new Access Token
                
                //Get the cached Refresh Token
                GOOGLE_REFRESH_TOKEN = new 
String(Config.GOOGLE_OAUTH_REFRESH_TOKEN);
                
                //Now we need to get a new Access Token using our 
previously cached Refresh Token
                RefreshTokenRequest refreshTokenRequest = new 
RefreshTokenRequest(httpTransport, 
                                                                 
jsonFactory, 
                                                                 new 
GenericUrl(Config.GOOGLE_TOKEN_SERVER_URL), 
                                                                 
GOOGLE_REFRESH_TOKEN);
                
                refreshTokenRequest.setClientAuthentication(new 
BasicAuthentication(Config.GOOGLE_CLIENT_ID, Config.GOOGLE_CLIENT_SECRET));
                
refreshTokenRequest.setScopes(Arrays.asList(Config.GOOGLE_SCOPE_PICASA));
                
                tokenResponse = refreshTokenRequest.execute();
                
                //Get and set the Refresn the Access Tokens
                GOOGLE_ACCESS_TOKEN = new 
String(tokenResponse.getAccessToken());
                }

            //At this point we have a valid Google Access Token
            //Let us access Picasa then!
            GoogleCredential credential = new GoogleCredential.Builder()
                    .setClientSecrets(Config.GOOGLE_CLIENT_ID, 
Config.GOOGLE_CLIENT_SECRET)
                    .setJsonFactory(jsonFactory)
                    .setTransport(httpTransport)
                    .build()
                    .setAccessToken(GOOGLE_ACCESS_TOKEN)
                    .setRefreshToken(GOOGLE_REFRESH_TOKEN);
            
            PicasawebService picasaWebSvc = new 
PicasawebService("GOOGLE_APP_NAME");
            picasaWebSvc.setOAuth2Credentials(credential);

            URL feedUrl = new 
URL("https://picasaweb.google.com/data/feed/api/user/"; + 
Config.PICASAWEB_LOGIN + "/albumid/" + Config.PICASAWEB_ALBUM_ID);
            
            MediaFileSource myMedia = new MediaFileSource(new 
File(fullpathToImage), "image/jpeg");
            PhotoEntry returnedPhoto = picasaWebSvc.insert(feedUrl, 
PhotoEntry.class, myMedia);
            
            resultingURL = returnedPhoto.getMediaContents().get(0).getUrl();
                
            if (resultingURL.toLowerCase().contains("please check your 
firewall") || resultingURL.toLowerCase().contains("error"))
                {
                throw new Exception("The Windows Firewall seems to be 
blocking the upload...");
                }
        
            System.out.println("...DONE!");
            System.out.println("Cover page URL: " + resultingURL);
            }
        catch (Exception e)
            {
            System.err.println("[BloggerImageUploader.java]: There was an 
error: " + e.getMessage());
            return "";
            }
        
    //The result
    return resultingURL;
    }

-- 
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