使用Google 相册API

阅读更多
在做GAE应用时,想上传图片在blog中使用,当然可以直接上传到相册,然后复制链接也是可行的,既然google相册提供了API,用api是不是更方便呢?上传完相片完马上就能知道相片地址了。

首先要下载gdata-java-client lib与相关依赖的lib

http://code.google.com/p/gdata-java-client/downloads/list



package sample.photos;

import java.io.File;
import java.net.URL;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;

public class UploadPhoto {

	public void createPhoto() {
		PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
		try {
			myService.setUserCredentials("[email protected]", "xxxx!@#");
			URL feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/xxxx?kind=album");
			UserFeed myUserFeed = myService.getFeed(feedUrl, UserFeed.class);
			for (AlbumEntry myAlbum : myUserFeed.getAlbumEntries()) {
				System.out.println(myAlbum.getTitle().getPlainText());
			}

			URL albumPostUrl = new URL("http://picasaweb.google.com/data/feed/api/user/xxxx/albumid/5358938369305614721");
			PhotoEntry myPhoto = new PhotoEntry();
			myPhoto.setTitle(new PlainTextConstruct("Puppies FTW"));
			myPhoto.setDescription(new PlainTextConstruct("Puppies are the greatest."));
			myPhoto.setClient("myClientName");
			MediaFileSource myMedia = new MediaFileSource(new File("F:/mm/1.jpg"), "image/jpeg");
			myPhoto.setMediaSource(myMedia);
			PhotoEntry returnedPhoto = myService.insert(albumPostUrl, myPhoto);
			System.out.println(returnedPhoto.getHtmlLink().getHref());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		UploadPhoto upLoadPhoto = new UploadPhoto();
		upLoadPhoto.createPhoto();

	}

}


这下操作google相册就方便很多了,晚上测试一下在GAE中能否使用这些API,顺利的话,下一步工作就更简单了。

你可能感兴趣的:(Google,GAE,Java,Gmail,.net)