Php 无法使用OAuth 2.0与网站管理员工具连接吗?

Php 无法使用OAuth 2.0与网站管理员工具连接吗?,php,oauth-2.0,google-webmaster-tools,Php,Oauth 2.0,Google Webmaster Tools,我设法通过Oauth 2连接到Analytics,但找不到使用网站管理员工具的方法。 我在以下网站获得了网站管理员工具的“范围”: 我在这里使用代码: 但是我不能工作。如果有人能指引我,我将不胜感激 注:今年5月XD以下代码将有助于通过Oauth流获取网站管理员工具API访问的访问令牌和刷新令牌 确保您在API控制台中提到的重定向Uri应该与您将在其中放置以下代码的文件名相同 例如,如果重定向uri为:-somesitename.com/google_oauth.php(使用http://或h

我设法通过Oauth 2连接到Analytics,但找不到使用网站管理员工具的方法。 我在以下网站获得了网站管理员工具的“范围”:

我在这里使用代码:

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


注:今年5月XD

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

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

例如,如果重定向uri为:-somesitename.com/google_oauth.php(使用http://或https://),则应将以下脚本置于:-google_oauth.php(路径:somesitename.com/google_oauth.php(使用http://或https://))


OAuth2授权码
授权代码:

访问令牌:

到期日:

刷新令牌:

然后,您可以使用此令牌查询网站管理员工具API中的数据


您也可以使用用于Oauth分析访问的相同代码,只需将此url替换为您在查询网站管理员工具API数据时用于分析的Oauth代码。

感谢您的回答sanju,我终于正确创建了Oauth,但找不到发送请求的url q:)
<?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>