授权需要oauth2 google picasa网络相册

时间:2015-06-04 17:01:57

标签: php authorization picasa

我在oauth2 google picasa API PHP中有授权问题。

我创建了以下代码:

$fields_param_string="code=".$_GET["code"]."&
client_id=XXXXXXXXXX.apps.googleusercontent.com&
client_secret=rY5sYxXXXXXXXXX&
redirect_uri=".urlencode("http://www.EXAMPLE.pl/upload.php").
"&grant_type=authorization_code";

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_param_string);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );   

然后我得到了正确的回复(我已经替换了一些数据而不是破解它):

"access_token": "ya29.iXXXXXXXXXXXXXX",
"token_type": "Bearer",
"expires_in": 3600,
"id_token":
"eyJhbXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

然后我运行以下内容来创建相册库:

$json = json_decode($response, true);
$url="https://picasaweb.google.com/data/feed/api/user/default";

$myvars="<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gphoto='http://schemas.google.com/photos/2007'>
            <title type='text'>".$wierszAD["THE_NAME_OF_ALBUM"]."</title>
            <summary type='text'>".$wierszAD[""]."</summary>
            <gphoto:location>Poland</gphoto:location>
            <gphoto:access>private</gphoto:access>
            <gphoto:timestamp>1152255600000</gphoto:timestamp>
            <media:group>
                <media:keywords>".$albumName_url."</media:keywords>
            </media:group>
            <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#album'></category>
    </entry>";

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type:
application/atom+xml"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );       

curl_close($ch);    

不幸的是我收到错误:

 Authorization required

如果我将以下代码替换为:

   $url="https://picasaweb.google.com/data/feed/api/user
  /MAIL_EXAMPLES_WITHOUT_@GMAIL.COM/full?access_token=".$json["access_token"];

我收到以下错误:

Token invalid - AuthSub token has wrong scope

所以我不知道如何在运行网址时进行授权,让我创建了picasa相册。

请帮帮我。

1 个答案:

答案 0 :(得分:-2)

使用以下功能代替curl

==================================

Refer  Above given library for connecting with google


    function googleConnect()
    { 

       require_once ('lib/Google/autoload.php'); 

       $client = new   Google_Client(); 

          ---Set your google crdentials here--- 

       $client->setClientId(CLIENT_ID);

       $client->setClientSecret(CLIENT_SECRET);

       $client->setRedirectUri(GOOGLE_REDIRECT_URI);

        ---You can set access type offline so that you--- 
        ---use user information when he is offline --- 

       $client->setAccessType('offline');

       $client->setApprovalPrompt('force');

         ---Add your scope here-- 

       $client->addScope("openid email");

       $client->addScope("https://picasaweb.google.com/data/");

       $client->addScope("https://www.googleapis.com/auth/userinfo.profile");

     return $client;



     }

------------------------------------------------------------------------

    ----get code and get acesss token---

 if (isset($_GET['code']))
 {


  $client->authenticate($_GET['code']);

  $_SESSION['access_token'] = $client->getAccessToken();

  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 

     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));


 }

------------------------------------------------------------------------


if (isset($_SESSION['access_token']) && $_SESSION['access_token'])

 { 

  $client->setAccessToken($_SESSION['access_token']);

 } 

else

 {

  $authUrl = $client->createAuthUrl();

 }

------------------------------------------------------------------------
if ($client->getAccessToken())

{ 

  $token = $_SESSION['access_token'] = $client->getAccessToken(); 

 $token = json_decode($token); $token = $token->access_token; 

}