开发者

How can I upload a photo to Picasa via the GData APIs, from .NET?

开发者 https://www.devze.com 2023-01-30 14:32 出处:网络
Similar toSimple example to upload photos into google picasa, which is about python; this one is about .NET.

Similar to Simple example to upload photos into google picasa, which is about python; this one is about .NET.

How to use the ClientL开发者_StackOverflow中文版ogin protocol, and then how to do the multipart/related form POST, as required by Google?


Apparently there is a class library that is packaged to do this. What I really wanted was the HTTP protocol required to do it. Using the Google API documentation for Picasa, this is what I figured out.

A request looks like this:

POST https://picasaweb.google.com/data/feed/api/user/default/albumid/default HTTP/1.1
GData-Version: 2.0
Authorization: GoogleLogin auth=DQAAAJgAAAD_....-tTM2ZliWDzXLA
Content-Type: multipart/related; boundary="1292107594356"
Host: picasaweb.google.com
Content-Length: 5415
Expect: 100-continue

Media multipart posting
--1292107594356
Content-Type: application/atom+xml

<entry xmlns='http://www.w3.org/2005/Atom'>
  <title>DesiredFileNameOnGoogle.jpg</title>
  <summary>uploaded from PicasaUpload</summary>
  <category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/photos/2007#photo'/>
</entry>
--1292107594356
Content-Type: image/jpeg

 (binary content here)
--1292107594356--

A response looks like this:

HTTP/1.1 201 Created
Expires: Sat, 11 Dec 2010 22:46:26 GMT
Date: Sat, 11 Dec 2010 22:46:26 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
Set-Cookie: _rtok=y6u5yQV84QUO; Path=/; Secure; HttpOnly
Set-Cookie: S=photos_html=lIi6RQVuq7F1iAQHMpMkUA; Domain=.google.com; Path=/; Secure; HttpOnly
Content-Type: application/atom+xml; charset=UTF-8; type=entry
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 2.0
ETag: "YD0qeyI."
Location: https://picasaweb.google.com/data/entry/api/user/1018683/albumid/55399652081/photoid/5549567010?authkey=Gv1sRgCJrQ5qeYxISo5gE
Content-Location: https://picasaweb.google.com/data/entry/api/user/1018683/albumid/55399652081/photoid/5549567010?authkey=Gv1sRgCJrQ5qeYxISo5gE
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked

And then the Atom payload in the response is like this:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:exif='http://schemas.google.com/photos/exif/2007' xmlns:app='http://www.w3.org/2007/app' xmlns:gphoto='http://schemas.google.com/photos/2007' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='&quot;YD0qeyI.&quot;'>
  <id>https://picasaweb.google.com/data/entry/user/1018683/albumid/5549217276399652081/photoid/5549498718938958882</id>
  <published>2010-12-11T18:49:18.000Z</published>
  <updated>2010-12-11T18:49:18.319Z</updated>
  <app:edited>2010-12-11T18:49:18.319Z</app:edited>
  <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#photo'/>
  <title>CropperCapture[30].jpg</title>
  <summary>woof!</summary>
  <content type='image/jpeg' src='https://lh5.googleusercontent.com/_-Mw_MhEEB_U/TQPHrq_XjCI/AAAAAAAAABQ/wu5AI7QfmWQ/CropperCapture%5B30%5D.jpg'/>
  <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://picasaweb.google.com/data/feed/api/user/1018683/albumid/5549217276399652081/photoid/5549498718938958882?authkey=Gv1sRgCJrQ5qeYxISo5gE'/>
  <link rel='alternate' type='text/html' href='http://picasaweb.google.com/1018683/DropBox?authkey=Gv1sRgCJrQ5qeYxISo5gE#5549498718938958882'/>
  <link rel='http://schemas.google.com/photos/2007#canonical' type='text/html' href='http://picasaweb.google.com/lh/photo/lzJ9Ya6NeZM8sPeVfFcn98QJQ0GDj3dCNP33YlpCH6E'/>
  <link rel='self' type='application/atom+xml' href='https://picasaweb.google.com/data/entry/api/user/1018683/albumid/5549217276399652081/photoid/5549498718938958882?authkey=Gv1sRgCJrQ5qeYxISo5gE'/>
  <link rel='edit' type='application/atom+xml' href='https://picasaweb.google.com/data/entry/api/user/1018683/albumid/5549217276399652081/photoid/5549498718938958882?authkey=Gv1sRgCJrQ5qeYxISo5gE'/>
  <link rel='edit-media' type='image/jpeg' href='https://picasaweb.google.com/data/media/api/user/1018683/albumid/5549217276399652081/photoid/5549498718938958882?authkey=Gv1sRgCJrQ5qeYxISo5gE'/>
  <link rel='http://schemas.google.com/photos/2007#report' type='text/html' href='http://picasaweb.google.com/lh/reportAbuse?uname=1018683&amp;aid=5549217276399652081&amp;iid=5549498718938958882'/>
  <gphoto:id>5549498718938958882</gphoto:id>
  <gphoto:albumid>5549217276399652081</gphoto:albumid>
  <gphoto:access>private</gphoto:access>
  <gphoto:width>441</gphoto:width>
  <gphoto:height>287</gphoto:height>
  <gphoto:size>26876</gphoto:size>
  <gphoto:checksum/>
  <gphoto:timestamp>1292093358000</gphoto:timestamp>
  <gphoto:imageVersion>20</gphoto:imageVersion>
  <gphoto:commentingEnabled>true</gphoto:commentingEnabled>
  <gphoto:commentCount>0</gphoto:commentCount>
  <gphoto:license id='0' name='All Rights Reserved' url=''>ALL_RIGHTS_RESERVED</gphoto:license>
  <exif:tags>
    <exif:imageUniqueID>abfde2a4ab9f9e177865ba4fd50e7edb</exif:imageUniqueID>
  </exif:tags>
  <media:group>
    <media:content url='https://lh5.googleusercontent.com/_-Mw_MhEEB_U/TQPHrq_XjCI/AAAAAAAAABQ/wu5AI7QfmWQ/CropperCapture%5B30%5D.jpg' height='287' width='441' type='image/jpeg' medium='image'/>
    <media:credit>User Name</media:credit>
    <media:description type='plain'>woof!</media:description>
    <media:keywords/>
    <media:thumbnail url='https://lh5.googleusercontent.com/_-Mw_MhEEB_U/TQPHrq_XjCI/AAAAAAAAABQ/wu5AI7QfmWQ/s72/CropperCapture%5B30%5D.jpg' height='47' width='72'/>
    <media:thumbnail url='https://lh5.googleusercontent.com/_-Mw_MhEEB_U/TQPHrq_XjCI/AAAAAAAAABQ/wu5AI7QfmWQ/s144/CropperCapture%5B30%5D.jpg' height='94' width='144'/>
    <media:thumbnail url='https://lh5.googleusercontent.com/_-Mw_MhEEB_U/TQPHrq_XjCI/AAAAAAAAABQ/wu5AI7QfmWQ/s288/CropperCapture%5B30%5D.jpg' height='188' width='288'/>
    <media:title type='plain'>CropperCapture[30].jpg</media:title>
  </media:group>
</entry>


Try that sample of code that is working for me i hope to work with you

GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("lh2", "Your application name");
        authFactory.Token = (string)HttpContext.Current.Session["token"];

PicasaService picasaService= new PicasaService(authFactory.ApplicationName);
        service.RequestFactory = authFactory;            
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(Constants.CurrentUser, albumId));

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
        System.IO.FileStream fileStream = fileInfo.OpenRead();

        PicasaEntry entry = (PicasaEntry)picasaService.Insert(postUri, fileStream, "image/jpeg", filePath);

        fileStream.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消