首页 > 解决方案 > 如何在 php 中向 Google RISC API 注册端点

问题描述

我一直在阅读并尝试实施使用跨帐户保护保护用户帐户的文档中的指示

到目前为止我所做的如下:

        JWT::$leeway = 60;

        $key = file_get_contents('location.json');

        $time = time();

        $payload = [
            "iss" => "account email",
            "sub" => "account email",
            "aud" => "https://risc.googleapis.com/google.identity.risc.v1beta.RiscManagementService",
            "iat" => $time,
            "exp" => $time + 3600,
        ];

        /**
         * IMPORTANT:
         * You must specify supported algorithms for your application. See
         * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
         * for a list of spec-compliant algorithms.
         */
        $jwt = JWT::encode($payload, $key);
        $decoded = JWT::decode($jwt, $key, ['HS256']);

        print_r($jwt);
        print_r($decoded);

        $client = new Client();

        try {
            $request = $client->post('https://risc.googleapis.com/v1beta/stream:update', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwt,
                    'Accept' => 'application/json',
                ],
                'form_params' => [
                    'delivery' => [
                        'delivery_method' => 'https://schemas.openid.net/secevent/risc/delivery-method/push',
                        'url' => 'https://test.myapp.com/webhooks/google',
                    ],
                    'events_requested' => [
                        'https://schemas.openid.net/secevent/oauth/event-type/tokens-revoked',
                    ],
                ],
            ]);

            $response = $request->getBody();

            dd($response);
        } catch (ClientException $exception) {
            dd($exception->getResponse()->getBody()->getContents());
        }

我面临的问题:

我从上面的代码中得到的错误是Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

尝试下面建议的编码RS256会给我一个错误UnexpectedValueException: Algorithm not allowed,我相信我确实缺乏有关 JWT 的必要知识并且在那里做错了什么。

标签: phpgoogle-apigoogle-oauthjwt-auth

解决方案


我还在研究如何通过此链接https://developers.google.com/identity/protocols/risc#java_1进行此跨帐户保护我假设您正在谈论这部分(生成授权令牌)https: //developers.google.com/identity/protocols/risc#auth_token

我正在使用 php,但查看页面中的 java 代码,它在您的代码中使用 RS256 而不是 HS256。如果您使用 php,那么您可以尝试使用 firebase php,它们有一个简单的 JWT 类可供您使用。https://github.com/firebase/php-jwt您可以只使用示例并将有效负载替换为您的,然后更改为 RS256。这就是我要尝试的方法,我可以让你知道它是否有效。


推荐阅读