13 messages in com.googlegroups.google-picasa-data-apiRe: inserting comments with PHP4
FromSent OnAttachments
voorhuid16 Apr 2008 11:18 
Jeff Fisher (Google)18 Apr 2008 13:25 
voorhuid19 Apr 2008 16:43 
Jochen Hartmann (Google)22 Apr 2008 06:26 
voorhuid22 Apr 2008 11:03 
Jeff Fisher (Google)23 Apr 2008 03:22 
voorhuid23 Apr 2008 13:13 
Jochen Hartmann (Google)24 Apr 2008 03:19 
voorhuid24 Apr 2008 04:47 
Jochen Hartmann (Google)24 Apr 2008 06:18 
voorhuid24 Apr 2008 08:08 
Jeff Fisher (Google)25 Apr 2008 03:25 
voorhuid25 Apr 2008 05:13 
Subject:Re: inserting comments with PHP4
From:Jochen Hartmann (Google) (api.@google.com)
Date:04/24/2008 06:18:38 AM
List:com.googlegroups.google-picasa-data-api

Mark,

I am not sure but to me the getToken() function looks strange. For instance why are you redirecting to google.com/base in your $redirect_url string ?

Perhaps you can rewrite it as this:

function getToken($nextUrl) {

$redirect_url = 'https://www.google.com/accounts/AuthSubRequest?' . 'scope=http%3A%2F%2Fpicasaweb.google.com %2Fdata&session=1&secure=0&next=' . $nextUrl;

print 'Before you get started, please <a href="' . $redirect_url . '">sign in</a> to your account.</a>';

}

Let me know if that helps Cheers, - Jochen

On Apr 24, 1:48 pm, voorhuid <voor@gmail.com> wrote:

For a second there I thought that would do the trick. I changed the scope to 'http://picasaweb.google.com/data'in the code above. Unfortunately, I still get the same error. (411)

On Apr 24, 12:20 pm, "Jochen Hartmann (Google)"

<api.@google.com> wrote:

Mark,

I think the problem has to do with the scope for your token request. It looks like you are trying to use a Google Base scoped token to access Picasa. Each of our APIs has their own scope that must be included with the token request. A token scoped for a particular API will not work on other APIs. You may want to copy and paste the AuthSub sample code from the Picasa Web Albums guide into your demo page:

Let me know if that helps Cheers, - Jochen

On Apr 23, 10:13 pm, voorhuid <voor@gmail.com> wrote:

Hi jeff,

Really appreciate the effort. I combined your and Jochem's snippets to make a simple as possible test page. Not very successful, although I am getting a different error now. This is the output:

Session Token: CLOuwte6BRDahfaS-f____8C The requested URL returned error: 411

(I changed a few chars in the token)

Here's the whole script:

<?php if ($_GET['token'] <> ""){ // single use token is set. exchange for session token, and post comment with it $token = $_REQUEST['token']; $upgrade_result = makeAuthSubRequest("https://www.google.com/accounts/ AuthSubSessionToken", $token); $token = substr($upgrade_result, strlen("Token=")); echo "Session Token: $token <br/>"; postItem($token);

} else { getToken(); }

function getToken() { $next_url = 'http://www.[mydomain].com/[mytestpage].php'; $redirect_url = 'https://www.google.com/accounts/AuthSubRequest? session=1'; $redirect_url .= '&next='; $redirect_url .= urlencode($next_url); $redirect_url .= "&scope="; $redirect_url .= urlencode('http://www.google.com/base/feeds/ items'); $redirect_url .= "&session=1";

print '<center>' . "\n"; print '<table style="width:50%;">' . "\n"; print '<tr><td>Before you get started, please <a href="'. $redirect_url.'">sign in</a> to your personal Google Base account.</ td></tr>' . "\n"; print '</table>' . "\n"; print '</center>' . "\n";

}

function makeRequest($URL, $headers) { $request = curl_init($URL); curl_setopt($request, CURLOPT_HTTPHEADER, $headers); curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($request); curl_close($request); return $result;

}

function makeAuthSubRequest($URL, $token) { $auth_header = array("Authorization: AuthSub token=$token"); return makeRequest($URL, $auth_header);

}

function postItem($sessionToken) { $itemsFeedURL = 'http://picasaweb.google.com/data/feed/api/user/ [username]/albumid/[albumid]/photoid/[photoid]'; $insertComment = "<entry xmlns='http://www.w3.org/2005/Atom'>" . "<content>This is what all the gentlemen are wearing this season.</content>" . "<category scheme=\"http://schemas.google.com/g/2005#kind\" " . "term=\"http://schemas.google.com/photos/2007#comment\"/></ entry>";

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $itemsFeedURL); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: AuthSub token="' . $sessionToken . '"', 'Content-Type: application/atom+xml' )); curl_setopt($ch, CURLOPT_POSTFIELDS, $insertComment);

$result = curl_exec($ch); echo curl_error($ch); print $result;}

?>

I am curious if you can get this to work, replacing [...] with your data.

Mark

On Apr 23, 12:23 pm, "Jeff Fisher (Google)" <api.@google.com> wrote:

Hi Mark,

I sat down yesterday and wrote some PHP curl stuff that worked with AuthSub to do the exchange:

function makeRequest($URL, $headers) {

$request = curl_init($URL); curl_setopt($request, CURLOPT_HTTPHEADER, $headers); curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($request); curl_close($request); return $result;

}

function makeAuthSubRequest($URL, $token) { $auth_header = array("Authorization: AuthSub token=$token"); return makeRequest($URL, $auth_header);

}

So if you are on a page that has been passed in a token query parameter after doing the redirect from Google Accounts you can do the exchange like this:

$token = $_REQUEST['token'];

$upgrade_result = makeAuthSubRequest("https://www.google.com/accounts/ AuthSubSessionToken", $token); $token = substr($upgrade_result, strlen("Token="));

echo "Session Token: $token <br/>";

Hope that helps! I have code to generate an AuthSub link as well if you want that.

Also, every time you click through the AuthSub page you do get a new one-time token and after exchanging it you get a new session token. You can, however, store the session token (in $_SESSION or in your database) if you want to re-use it again the next time a user accesses the page.

Cheers, -Jeff

On Apr 22, 8:04 pm, voorhuid <voor@gmail.com> wrote:

Jochem,

Tank you for your effort. I scanned the code you used, and the only difference I could find is that you are using the albumid i.s.o. albumname like I do. I tried to refer to the album-id as well this time, but still no success!

Could it be the session token I am using? I noticed that both the token and session token are different on each request. I would figure the request would return always the same token, as the same algorithm is used with the same input every time. On the other hand, if no valid token could be generated, one would assume google returns an error. I am really kinda stuck here. If you want my code for retrieving a session token, please let me know.

Mark

On Apr 22, 3:26 pm, "Jochen Hartmann (Google)"

<api.@google.com> wrote:

Hi,

I was not able to replicate the error that you were getting. Running this on my development machine, which uses php 5.2 works perfectly. Here is my code:

function postItem($sessionToken) {

$itemsFeedURL = 'http://picasaweb.google.com/data/feed/api/user/ test.api.jhartmann/albumid/5192046855371220017/photoid/ 5192046932680631378';

$insertComment = "<entry xmlns='http://www.w3.org/2005/Atom'>" . "<content>This is what all the gentlemen are wearing this season.</content>" . "<category scheme=\"http://schemas.google.com/g/2005#kind\" " . "term=\"http://schemas.google.com/photos/2007#comment\"/></ entry>";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $itemsFeedURL); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: AuthSub token="' . $sessionToken . '"', 'Content-Type: application/atom+xml' )); curl_setopt($ch, CURLOPT_POSTFIELDS, $insertComment);

$result = curl_exec($ch); echo curl_error($ch); print $result;

}

And here is the comment entry that it produced:

I ran this from a session based page that uses AuthSub authentication. More documentation is available at the link below:

Cheers, - Jochen

On Apr 20, 1:44 am, voorhuid <voor@gmail.com> wrote:

Thank you for replying. The devkey came from some gdata example I found somewhere. I took it out and followed your suggestion on returning the curl error. Now I get this:

The requested URL returned error: 500

This really makes me feel like a newbie.. I must be missing something obvious. Here's the code I used:

function postItem($sessionToken) {

$itemsFeedURL = "http://picasaweb.google.com/data/feed/api/user/12345/ album/23456/photoid/5180987208362827106";

$insertComment = "<entry xmlns='http://www.w3.org/2005/Atom'> <content>This is what all the
gentlemen are wearing this season.</content> <category
scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/photos/2007#comment\"/> </entry>";

...

read more »