(401) Unauthorized when trying to insert a moment using the Google+ API
使用下面的代码,我成功接收了一个令牌,并且使用此令牌也可以获得用户详细信息,但是当我尝试在用户墙上张贴/插入时,我会看到以下错误消息
带有消息
的
在登录页面上使用我的网站进行用户登录时,我获得了用户令牌,我要求以下权限
1 2 3 | $client->addScope("email"); $client->addScope("https://www.googleapis.com/auth/plus.stream.write"); $client->addScope("https://www.googleapis.com/auth/plus.login"); |
如果我print_r($ tokenInfo);您将在登录时看到我要求的所有范围。
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | session_start(); require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/autoload.php'); $client_id = 'my_client_id'; $client_secret = 'my_secret_key'; $redirect_uri = 'my_redirect_url'; // code to post in google plus start here // $client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); if (isset($_SESSION['access_token_gp'])) { // Verify the token $token = json_decode($_SESSION['access_token_gp']); $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$token->access_token; $req = new Google_Http_Request($reqUrl); $tokenInfo = get_object_vars(json_decode($client->getAuth()->authenticatedRequest($req)->getResponseBody())); if($tokenInfo['expires_in'] <= 0){ $client->authenticate($_SESSION['access_token_gp']); $_SESSION['access_token_gp'] = $client->getAccessToken(); } else { $client->setAccessToken($_SESSION['access_token_gp']); } $plusservicemoment = new Google_Service_Plus_Moment(); $plusservicemoment->setType("http://schemas.google.com/AddActivity"); $plusService = new Google_Service_Plus($client); $item_scope = new Google_Service_Plus_ItemScope(); $item_scope->setId($tokenInfo['user_id']); $item_scope->setType("http://schemas.google.com/AddActivity"); $item_scope->setName("The madexme Platform"); $item_scope->setDescription("A page that describes just how madexme is work!"); //$item_scope->setImage("full image path here"); $plusservicemoment->setTarget($item_scope); $result = $plusService->moments->insert('me','vault',$plusservicemoment); //print_r($result); } // code to post in google plus end here // |
确保您具有来自GitHub的最新客户端库。您的Oauth2连接有问题。此代码部分是从我的Google Google Calendar API教程转换而来的。我现在没有能力对其进行测试。但这应该很接近。今晚我将对其进行测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <?php require_once 'Google/Client.php'; require_once 'Google/Service/Plus.php'; session_start(); $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("AIzaSyBBH88dIQPjcl5nIG-n1mmuQ12J7HThDBE"); $client->setClientId('2046123799103-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com'); $client->setClientSecret('6s4YOx3upyJhtwnetovfK40e'); $client->setRedirectUri('http://localhost/google-api-php-client-samples/Calendar/oauth2Pure.php'); $client->setAccessType('offline'); // Gets us our refreshtoken $client->setScopes(array(https://www.googleapis.com/auth/plus.login')); //For loging out. if (isset($_GET['logout'])) { unset($_SESSION['token']); } // Step 2: The user accepted your access now you need to exchange it. if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } // Step 1: The user has not authenticated we give them a link to login if (!isset($_SESSION['token'])) { $authUrl = $client->createAuthUrl(); print"Connect Me!"; } // Step 3: We have access we can now create our service if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); print"LogOut"; $service = new Google_Service_Plus($client); $moment = new Google_Moment(); $moment->setType('http://schemas.google.com/AddActivity'); $itemScope = new Google_ItemScope(); $itemScope->setUrl('https://developers.google.com/+/plugins/snippet/examples/thing'); $moment->setTarget($itemScope); $plus->moments->insert('me', 'vault', $moment); ?> |
再次希望您能理解,这不会显示在用户的Google页面/时间轴上。