【问题标题】:Insufficient Permission in Mirror API Insert Account using PHP使用 PHP 的镜像 API 插入帐户中的权限不足
【发布时间】:2014-08-08 10:15:15
【问题描述】:

我有用于插入用户帐户的 Google PHP Mirror API 的 PHP 代码。

    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}

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

【问题讨论】:

    标签: php google-glass


    【解决方案1】:

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

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

    【讨论】:

    • 尝试仅添加此范围。给出同样的错误。我无法找出原因。在创建服务对象之前,我将 setAssertionCredentials 调用到客户端对象。调用这个方法的地方是否正确?
    • 您能否确认您的 API 项目已启用“Google Mirror API”?还要确保将“service-email-here”替换为 API 控制台中为正确客户端 ID 提供的电子邮件地址。
    • “Google Mirror API”已启用。在 API 控制台中,我有 2 个客户端 ID 和 2 个电子邮件地址。一个用于“Web 应用程序的客户端 ID”,用于 OAuth,另一个用于“服务帐户”,用于镜像 API。我们正在整个应用程序中创建一个客户端。我想这就是问题的根源。这怎么可能解决?
    • 我使用单独的客户端调用了镜像帐户插入方法,该客户端具有服务客户端 ID 和服务电子邮件 ID。我现在从镜像插入方法得到空响应而不是错误!在将 postBody 添加到同一方法时,我需要进一步的帮助。谢谢。
    【解决方案2】:

    调用插入帐户方法需要单独的服务客户端对象。代码可以看这里:http://goo.gl/DVggO6

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-24
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2016-05-04
      • 1970-01-01
      • 2016-01-12
      相关资源
      最近更新 更多