使用PHP进行GDK身份验证

时间:2015-01-06 07:42:36

标签: php google-gdk google-mirror-api

这是我的测试代码:

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

<?php

include_once "templates/base.php";
session_start();

require_once realpath(dirname(__FILE__) . '/../autoload.php');

function get_service_client(){
 $service_client_id="540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc.apps.googleusercontent.com";
 $service_email="540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc@developer.gserviceaccount.com";
 $app_name="Eyenotes";
 $key_file_location='C:/Users/Yuan/Desktop/eyenotes-81e92b7df1f1.p12';
 $browser_api_key="AIzaSyAplINlgfGWGJ8pD1Bxlr8wg7i8ZNVOS1A";

$client = new Google_Client();
$client->setApplicationName("Eyenotes"); // Set your application name
$client->setClientId("540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc.apps.googleusercontent.com");
$client->setDeveloperKey("AIzaSyAplINlgfGWGJ8pD1Bxlr8wg7i8ZNVOS1A");
$key = file_get_contents("C:/Users/Yuan/Desktop/eyenotes-81e92b7df1f1.p12");
$cred = new Google_Auth_AssertionCredentials(
$service_email,array('https://www.googleapis.com/auth/glass.thirdpartyauth'),$key
);
$client->setAssertionCredentials($cred);
return $client;
}

function insert_account($service,$userToken, $email)
{
$accountType='com.eyenotes';
$userDataArray= array();
$userData1= new Google_Service_Mirror_UserData();
$userData1->setKey('email');
$userData1->setValue("lensZheng@gmail.com");
$userDataArray[]=$userData1;
$authTokenArray= array();
$authToken1= new Google_Service_Mirror_AuthToken();
$authToken1->setAuthToken('randomtoken');
$authToken1->setType('randomType');
$authTokenArray[]=$authToken1;
$postBody = new Google_Service_Mirror_Account();
$postBody->setUserData($userDataArray);
$postBody->setAuthTokens($authTokenArray);
try {
$account = $service->accounts->insert($userToken, $accountType, $email, $postBody);
echo $account;
} catch (Exception $e) {

}
}
$service_client= get_service_client();
$mirrorService = new Google_Service_Mirror($service_client);
insert_account($mirrorService, "6164da1732ea09b4", "ccfsz.public@gmail.com");
?>

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

这是我得到的例外:

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

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/mirror/v1/accounts/6164da1732ea09b4/com.eyenotes/ccfsz.public%40gmail.com?key=AIzaSyDxnl16J8Eiwv00k_1yzw_VoRr9wD32qao: (400) Invalid Value' in D:\MySoftware\wamp\www\insert\src\Google\Http\REST.php on line 111
Google_Service_Exception: Error calling POST https://www.googleapis.com/mirror/v1/accounts/6164da1732ea09b4/com.eyenotes/ccfsz.public%40gmail.com?key=AIzaSyDxnl16J8Eiwv00k_1yzw_VoRr9wD32qao: (400) Invalid Value in D:\MySoftware\wamp\www\insert\src\Google\Http\REST.php on line 111

========================================= 参数说明:

帐户类型是我提供给谷歌。

用户令牌是我从身份验证网址获取的,我提供给谷歌。

在开发者控制台中创建新密钥时会生成浏览器API密钥。

请告诉我代码中的问题是什么?

感谢。

我抓住了异常并且它没有再次引发异常,但我仍然无法使用mAccountManager.getAccountByType(&#34; com.eyenotes&#34;)检索帐户信息。在谷歌开发者控制台中,它显示我已成功请求。另一个,我在切换后无法获得我的申请&#34; ON&#34;在Glassware.Now我正在尝试重置我的玻璃,但两个小时后,我没有得到任何表明我重置成功的信息。 (现在我的申请不公开)

1 个答案:

答案 0 :(得分:0)

我想知道很多事情......首先,错误中的路径不在您的代码中。因此无法确定错误实际来自何处。

关于GDK中的身份验证可能还有其他问题,只是为了让您了解......您可以暂时存储随该请求发送的userToken查询参数(不要永久存储它),然后通过您运行的任何后端对用户进行身份验证。然后,您可能会要求其Google帐户的适当范围。完成后,您可以在PHP中正常创建镜像服务,然后使用Accounts集合:

    // $myClient would contain the typical Google_Client() setup
// See PHP quickstart for example
$myMirrorService = new Google_Service_Mirror($myClient);

// Set your inputs to insert() as needed
$accounts = $myMirrorService->accounts->insert($userToken, $accountType, $accountName, $postBody);

此外,您只能在APK登陆MyGlass后进行测试并使用此API(在审核过程中会发生)。我还建议Mirror API PHP Quickstar作为一个起点,以了解如果您尚未这样做,身份验证的工作原理。