3

I'm setting up the Android Management API with the Google API PHP Client but I figured that my authentication config I provided to the client has no impact when I'm sending a request.

I tested if the credentials file exists and if syntax errors inside the file are handled. I followed the error message link. I've searched the web several times, reached out to the documentation and the php doc inside the library but I couldn't figure it out.

$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();
}

Expected: signupUrl Object Actual: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

4

1 回答 1

1

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

您可以通过设置重定向 URL 并将用户定向到注册 URL 来执行此操作。

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

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

如果您仍处于开发阶段,您可以使用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,它应该为您提供可以手动输入的代码。

于 2019-06-13T09:30:05.813 回答