当前位置: 首页 > 知识库问答 >
问题:

Google API-使用服务目录

尉迟墨竹
2023-03-14

我有一个应用程序在谷歌API控制台。它启用了管理SDK,也启用了市场SDK。当我试图从某个域获取用户时,它总是显示一条消息“错误调用gethttps://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com:(403)不授权访问此资源/api”。我得到的代码是这样的:

    $client = new Google_Client();
    $client->setApplicationName("Client_User_Feed");            

    $key = file_get_contents('/path/to/key/key-file-privatekey.p12');
    $cred = new Google_Auth_AssertionCredentials(
        '{code}@developer.gserviceaccount.com',
        array('https://www.googleapis.com/auth/admin.directory.user'),
        $key
    );
    $client->setAssertionCredentials($cred);
    $service = new Google_Service_Directory($client);           

    $users = $service->users->listUsers(array('domain' => 'mydomain.com'));

我该如何解决这个问题?

共有1个答案

海嘉赐
2023-03-14

您需要使用以下内容来模拟管理员用户:

SUB=AdminUser/Code>

获取userid的示例代码:

$client_id = '{code}.apps.googleusercontent.com';  //Client ID from Developers Console
$service_account_name = '{code}@developer.gserviceaccount.com'; //Email Address from Developers Console
$key_file_location = '{path}{file}.p12';    //Path to the P12 key downloaded from Developers Console
$impersonateUser = 'standarduser@domain.com';   //The user's account we are fetching information from   

    try {
    $client = new Google_Client();  //Instantiate the Google Client
    $client->setApplicationName("ApplicationName");

    $adminService = new Google_Service_Directory($client);

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(  //Instantiate the Auth class
        $service_account_name,
        array('https://www.googleapis.com/auth/admin.directory.user'),         //Set the scope
        $key
    );

    $adminUser = 'admin@domain.com';
    $cred->sub = $adminUser;  //The sub function of Auth lets us impersonate a user so that our service account ($client_id) can act on the user's behalf

    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }

    $getUser = getUserId($adminService, $impersonateUser);
    $impersonateUser = $getUser['primaryEmail'];

    if (isset($impersonateUser) && !empty($impersonateUser)) {
        $_SESSION['gmailUserID'] = $impersonateUser;
    }   
    //echo $_SESSION['gmailUserID'] . "<br />";
    } catch (Exception $e) {
        LogErr($e);
    }

function getUserId($adminService, $impersonateUser) {
    try {
    $userId = $adminService->users->get($impersonateUser);
    return $userId;
    } catch (Exception $e) {
    LogErr($e);
    }
}
 类似资料:
  • 我正在使用npm包googleapis:^100.0.0,我得到了这个错误: 错误:令牌使用得太晚,1653569732.911 我的工作流程是这样的: > 我在第一个应用程序授权时从谷歌誓言获得access_token、refresh_token和id_token。 const scopes =[" https://www . Google APIs . com/auth/userinfo .

  • 我们有一个ftp服务器,下面只有很少的目录和文件,我可以通过浏览器连接并成功访问目录。但是,当使用同一服务器时:具有凭据的端口无法连接。还尝试使用JSR233采样器列出文件,但没有成功。 请引导。 TestPlan:FTP请求默认值(服务器IP和端口:21) 对于FTP请求,获取如下错误:响应消息:java。木卫一。FileNotFoundException:(系统找不到指定的路径)请求正在进行:

  • 我对GoogleAPI有些问题。 这是错误。 条件:jdk 1.8,Tomcat 8.5 请帮我解决这个错误。

  • 部署web服务器首先要创建一个 请求处理器(request handler)。 请求处理器可以是协程方法也可以是普通方法,它只有一个用于接受Request实例对象的参数,之后会返回Response实例对象: from aiohttp import web async def hello(request): return web.Response(text="Hello, world")

  • 我一步一步地遵循这篇论文:https://richonrails.com/articles/google-authentication-in-ruby-on-rails/我创建了一个全新的rails应用程序来测试。但是我一直得到同样的错误:错误:invalid_request 重定向uri的参数值无效:不允许原始IP地址:http://0.0.0.0:3000/auth/google_oauth2

  • 问题内容: 下面的代码有任何错误吗?以下代码无法提供多个目录服务。当我访问localhost:9090 / ide时,服务器将返回404错误。 当我这样更改代码时, 它会按我预期的那样工作。 问题答案: 使用中的问题之一是请求路径用于构建文件名,因此,如果要从根目录以外的任何地方提供服务,则需要剥离到该处理程序的路由前缀。 标准库为此提供了一个有用的工具,但该工具只能在s上使用,而不能在s 上使用