镜像API中的权限不足使用PHP插入帐户

时间:2014-08-08 10:15:16

标签: php google-glass

我有PHP代码,谷歌PHP镜像API插入用户帐户。

    if(isset($_GET['userToken'])){
        $userToken=$_GET['userToken'];

        $key = file_get_contents($key_file_location);   
        $cred = new Google_Auth_AssertionCredentials(
            'service-email-here',
             array('https://www.googleapis.com/auth/glass', 'https://www.googleapis.com/auth/glass.thirdpartyauth'),
            $key
        );          
        $client->setAssertionCredentials($cred);

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

        insert_account($mirrorService, $userToken, $email, $api_email);
        exit();
    }       


function insert_account($service,$userToken, $email, $api_email)
{
$accountType='package-name-here';
$accountName='service-email-here';

$authtoken=null;    
$postBody = new Google_Service_Mirror_Account();
$postBody->setAuthTokens($authtoken);
$userdata=array("email"=>$email);
$postBody->setUserData($userdata);

try {   
    $account = $service->accounts->insert($userToken, $accountType, $accountName, $postBody);
    return $account;
} catch (Exception $e) {
    echo "exception: ".$e;
    return null;
}
}

我收到错误消息:

exception: exception 'Google_Service_Exception' with message 'Error calling POST      https://www.googleapis.com/mirror/v1/accounts/usertokenhere/package-name-here/service-email-here: (403) Insufficient Permission' in app-path/Google/Http/REST.php:79
Stack trace:
#0 app-path/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 app-path/Google/Client.php(508): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 app-path/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 app-path/Google/Service/Mirror.php(409): Google_Service_Resource->call('insert', Array, 'Google_Service_...')
#4 app-path/util.php(52): Google_Service_Mirror_Accounts_Resource->insert('8ce7d2ffbe6f693...', 'com.tenpearls.l...', '568533774877-an...', Object(Google_Service_Mirror_Account))
#5 app-path/add-your-own-camera.php(59): insert_account(Object(Google_Service_Mirror), '8ce7d2ffbe6f693...', 'safdar.tp@gmail...', '568533774877-an...')
#6 {main}

为什么我收到的权限不足错误?我添加了使用mirror api添加帐户所需的范围https://www.googleapis.com/auth/glass.thirdpartyauth。 我需要使用镜像API插入用户帐户。

2 个答案:

答案 0 :(得分:1)

您不需要范围https://www.googleapis.com/auth/glass。实际上,这根本不是一个有效的范围。

使用服务帐户在设备上添加帐户所需的唯一范围是https://www.googleapis.com/auth/glass.thirdpartyauth

答案 1 :(得分:0)

调用insert account方法需要单独的服务客户端对象。可在此处查看代码:http://goo.gl/DVggO6