Google MyBusiness access from webservers without redirects

烂漫一生 提交于 2019-12-11 06:45:36

问题


As detailed in my question to the the Google API team, I would like to work out a way to avoid redirects.

In theory this should be possible as an Authentication Code from one client (JavaScript) should be agnostic of the client and thus it should work if passed to the PHP client to fetch the access and refresh tokens.

Steps in theory: 1. Client gets an authorization code 2. Client exchanges the authorization code for the access and refresh tokens

How am I attempting this?

  1. Run the JavaScript client to get the Authentication token
GoogleAuth = gapi.auth2.getAuthInstance()
GoogleAuth.grantOfflineAccess({
    scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.business.manage https://www.googleapis.com/auth/plus.me openid email profile'
}).then(function (resp) {
    var auth_code = resp.code;
    console.log("AuthCode:" + auth_code)
})

At this point i get a code, not sure if this is an authorization code or access token but i cannot see any other function in the Javascript library that is Authorization token explicit/specific.

  1. Use the Authentication Token in the PHP API Library
$error = $request->get('error');
        $code  = $request->get('code');

        if($error){
            throw new Exception('Error from authenticating ' . $error);
        }

        $client = new \Google_Client();
        $client->setAuthConfig(getcwd() . '/../client_secret.apps.googleusercontent.com.json');
        $client->setAccessType("offline");        // offline access
        $client->setIncludeGrantedScopes(true);   // incremental auth
        $client->addScope(
            array(
                'https://www.googleapis.com/auth/userinfo.email',
                'https://www.googleapis.com/auth/userinfo.profile',
                'https://www.googleapis.com/auth/plus.business.manage'
            )
        );

        $client->setRedirectUri('http://myserver.com/code');
        $client->setApprovalPrompt('force');

        $client->fetchAccessTokenWithAuthCode($code);
        $accessToken = $client->getAccessToken();

        return new Response(
            "<html><body>Authenticated with code : " . $code . "<br/>\n\n".
            " Access Token is : ". var_export($accessToken, true) . "</body></html>"
        );
  1. Access token is still null. the line $accessToken = $client->getAccessToken(); returns null or false.

This works in the full PHP based version but the PHP version is based on creating a link that the user needs to follow, the user is then on the Google server and can approve the app, when approved the user is redirected back to the app. Then the app receives the Authentication code.

I just would like to avoid redirects due to the architecture of single pages apps or just preference. The only alternative I can think of is to open a popup and notify the original window when access and refresh codes are returned so that the PHP client can go on querying the API, but it is an ugly solution IMHO.

Is there another way to get an authorization code that works on PHP but obtained from JavaScript?

来源:https://stackoverflow.com/questions/54349749/google-mybusiness-access-from-webservers-without-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!