【问题标题】:Is impossible to connect with webmaster tools using OAuth 2.0?无法使用 OAuth 2.0 连接网站管理员工具吗?
【发布时间】:2013-11-10 22:12:08
【问题描述】:

我设法通过 Oauth 2 与 Analytics 连接,但找不到使用网站管理员工具的方法。 我在以下位置获得了网站管理员工具的“范围”:https://developers.google.com/oauthplayground/

我在这里使用代码: https://code.google.com/p/google-api-php-client/

但我不能工作。如果有人能指导我将不胜感激。

PS:今年五月XD

【问题讨论】:

    标签: php oauth-2.0 google-search-console


    【解决方案1】:

    以下代码将有助于通过 Oauth Flow 获取网站管理员工具 API 访问的访问令牌和刷新令牌

    确保您在 API 控制台中提到的重定向 Uri 应该与您将在其中放置以下代码的文件名相同。

    例如。如果重定向 uri 是:-somesitename.com/google_oauth.php(带有 http:// 或 https://),则应将以下脚本放在:- google_oauth.php(路径:somesitename.com/google_oauth.php(带有http:// 或 https://))

    <?php
    
        $OAuth = array(
            'oauth_uri' => 'https://accounts.google.com/o/oauth2/auth',
            'client_id' => '#clientId',
            'client_secret' => '#clientSecret',
            'access_type' => 'offline',
            'redirect_uri' => 'http://somesite.com/google_oauth.php',   //this url should be same as you had registered in your api console as redirect uri()
            'oauth_token_uri' => 'https://accounts.google.com/o/oauth2/token'
    
        );
        $token = array(
            'access_token' => '',
            'token_type' => '',
            'expires_in' => '',
            'refresh_token' => ''
        );
    
        $title = 'No Code';
        $AuthCode = 'Null';
    
        // see if error parameter exisits
        $error = _get_url_param($_SERVER['REQUEST_URI'], 'error');
        if ($error != NULL)
        {   // this means the user denied api access to GWMTs
            $title = $error;
        }
        else
        {   // does the code parameter exist?
            $AuthCode = _get_url_param($_SERVER['REQUEST_URI'], 'code');
            if ($AuthCode == NULL)
            {   // get authorization code
                $OAuth_request = _formatOAuthReq($OAuth, "https://www.google.com/webmasters/tools/feeds/");
    
                header('Location: ' . $OAuth_request);
                exit; // the redirect will come back to this page and $code will have a value
            }
            else
            {
                $title = 'Got Authorization Code';
                // now exchange Authorization code for access token and refresh token
                $token_response = _get_auth_token($OAuth, $AuthCode);
                $json_obj = json_decode($token_response);
                $token['access_token'] = $json_obj->access_token;
                $token['token_type'] = $json_obj->token_type;
                $token['expires_in'] = $json_obj->expires_in;
                $token['refresh_token'] = $json_obj->refresh_token;
                echo 'access_token = ' . $json_obj->access_token;
            }
        }
    
        function _get_auth_token($params, $code)
        {
            $url = $params['oauth_token_uri'];
    
            $fields = array(
                'code' => $code,
                'client_id' => $params['client_id'],
                'client_secret' => $params['client_secret'],
                'redirect_uri' => $params['redirect_uri'],
                'grant_type' => 'authorization_code'
            );
            $response = _do_post($url, $fields);
            return $response;
        }
    
        function _do_post($url, $fields)
        {
            $fields_string = '';
    
            foreach ($fields as $key => $value)
            {
                $fields_string .= $key . '=' . $value . '&';
            }
            $fields_string = rtrim($fields_string, '&');
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, count($fields));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            $response = curl_exec($ch);
            curl_close($ch);
    
            return $response;
        }
    
        function _formatOAuthReq($OAuthParams, $scope)
        {
            $uri = $OAuthParams['oauth_uri'];
            $uri .= "?client_id=" . $OAuthParams['client_id'];
            $uri .= "&redirect_uri=" . $OAuthParams['redirect_uri'];
            $uri .= "&scope=" . $scope;
            $uri .= "&response_type=code";
            $uri .= "&access_type=offline";
    
    
            return $uri;
        }
    
        function _get_url_param($url, $name)
        {
            parse_str(parse_url($url, PHP_URL_QUERY), $params);
            return isset($params[$name]) ? $params[$name] : null;
        }
    
        function _get_refresh_token($params, $code)
        {
            $url = $params['oauth_token_uri'];
    
            $fields = array(
                'code' => $code,
                'client_id' => $params['client_id'],
                'client_secret' => $params['client_secret'],
                'refresh_token' => $token['refresh_token'],
                'grant_type' => 'refresh_token'
            );
            $response = _do_post($url, $fields);
            return $response;
        }
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title><?= $title; ?></title>
            </head>
            <body>
                <h1>OAuth2 Authorization Code</h1>
                <p>Authorization Code: <?= $AuthCode; ?></p>
                <p>access token: <?= $token['access_token']; ?></p>
                <p>expires in: <?= $token['expires_in']; ?></p>
                <p>refresh token: <?= $token['refresh_token']; ?></p>
                <p></p>
    
            </body>
        </html>
    

    然后您可以使用此令牌查询网站管理员工具 API 以获取数据。

    您也可以使用您用于 Oauth 分析访问的相同代码,只需在查询网站管理员工具 API 数据时用于分析的 oauth 代码中将此 url:https://www.googleapis.com/auth/analytics.readonly 替换为 https://www.google.com/webmasters/tools/feeds/

    【讨论】:

    • 感谢您的回答 sanju,我终于正确地进行了 oauth,但找不到发送请求 q 的 url :)
    猜你喜欢
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 2011-03-17
    • 2013-06-27
    • 2012-03-20
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多