【问题标题】:Request has insufficient authentication请求的身份验证不足
【发布时间】:2019-06-13 08:01:51
【问题描述】:

我正在使用 Google API PHP 客户端设置 Android 管理 API,但我认为我提供给客户端的身份验证配置在我发送请求时没有影响。

我测试了凭据文件是否存在以及文件中的语法错误是否得到处理。我按照错误消息链接进行操作。我已经在网上搜索了好几次,找到了库中的文档和 php 文档,但我无法弄清楚。

$client = new \Google_Client();
$client->setApplicationName('SecretName');
$client->setAuthConfig(x::getRootDir() . '/modules/package-androidmanagement/credentials2.json');
$client->addScope(Google_Service_AndroidManagement::ANDROIDMANAGEMENT);
$am = new \Google_Service_AndroidManagement($client);

try {
 $signupUrl = $am->signupUrls->create(['projectId' => $this->projectId, 'callbackUrl' => x::getDomain()]);
} catch (Exception $exception) {
 echo $exception->getMessage();
}

预期:signupUrl 对象 实际:请求缺少所需的身份验证凭据。预期的 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。见https://developers.google.com/identity/sign-in/web/devconsole-project

【问题讨论】:

    标签: google-api-php-client android-management-api


    【解决方案1】:

    在您能够生成任何组织注册网址(或执行任何 api 调用)之前,您需要对自己进行身份验证。

    您可以通过设置重定向 URL 并将用户定向到注册 URL 来做到这一点。

    $client->setRedirectUri('https://example.com/register');
    $authUrl = $client->createAuthUrl();
    

    不过,这需要完整设置 oauth 流程。这意味着您的 oauth 同意屏幕已通过 google 验证(可能需要数周时间),并且您已为重定向 url 设置了各种允许的域。

    如果您仍处于开发阶段,您可以通过quickstart notebook by google提供的oauth同意屏幕:

    # This is a public OAuth config, you can use it to run this guide but please use
    # different credentials when building your own solution.
    CLIENT_CONFIG = {
        'installed': {
            'client_id':'882252295571-uvkkfelq073vq73bbq9cmr0rn8bt80ee.apps.googleusercontent.com',
            'client_secret': 'S2QcoBe0jxNLUoqnpeksCLxI',
            'auth_uri':'https://accounts.google.com/o/oauth2/auth',
            'token_uri':'https://accounts.google.com/o/oauth2/token'
        }
    }
    SCOPES = ['https://www.googleapis.com/auth/androidmanagement']
    

    使用它来替换您的 oauth json 配置中的数据。

    通过不设置重定向 uri,它应该为您提供可以手动输入的代码。

    【讨论】:

    • 完美配合添加:$client->setAccessType('offline')
    猜你喜欢
    • 2017-08-09
    • 2021-08-07
    • 2021-01-04
    • 2022-08-22
    • 2021-10-20
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多