Php 403-请求的身份验证作用域不足

Php 403-请求的身份验证作用域不足,php,google-analytics-api,google-ads-api,google-authentication,Php,Google Analytics Api,Google Ads Api,Google Authentication,我想链接并查看与谷歌Adwords链接的分析帐户 使用的程序: 使用以下url验证作用域为“AnAnAnlytics和Adwords”的google帐户 获取身份验证响应后,创建Google analytics服务对象 谷歌广告链接API抛出错误“前置任务不足”附截图 脚本: <?php //function to authenticate google account and create analytics service object function googleAuth(){

我想链接并查看与谷歌Adwords链接的分析帐户

使用的程序:

  • 使用以下url验证作用域为“AnAnAnlytics和Adwords”的google帐户
  • 获取身份验证响应后,创建Google analytics服务对象
  • 谷歌广告链接API抛出错误“前置任务不足”附截图
  • 脚本:

    <?php
    
    //function to authenticate google account and create analytics service object
    function googleAuth(){
    
            if (!empty($code)) {
    
                            $postFields = 'client_id=' . Configure::read('GOOGLE_OAUTH_CLIENT_ID') . '&client_secret=' . Configure::read('GOOGLE_OAUTH_CLIENT_SECRET') . '&code=' . $code . '&grant_type=authorization_code&redirect_uri=' . Configure::read('GOOGLE_OAUTH_REDIRECT_URI');
    
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    
                            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
                            $Rec_Data = curl_exec($ch);
    
                            if (curl_exec($ch) === false) {
                                    return $Rec_Data;
                            }
    
                            $Rec_Data = json_decode($Rec_Data, true);
    
                            if (isset($Rec_Data['refresh_token'])) {
                                    
                                    try {
    
                                            $credentials = array('client_id' => Configure::read('GOOGLE_OAUTH_CLIENT_ID'), 'client_secret' => Configure::read('GOOGLE_OAUTH_CLIENT_SECRET'), 'redirect_uris' => array(Configure::read('GOOGLE_OAUTH_REDIRECT_URI')));
    
                                            $client = new \Google_Client($credentials);
                                                    
                                                    $client->addScope(\Google_Service_Analytics::ANALYTICS_READONLY);
                                                    $client->setAccessToken($Rec_Data['access_token']);
    
                                                    // Create an authorized analytics service object.
                                                    $analytics = new \Google_Service_Analytics($client);
    
                                            
    
    
                                    } catch (Exception $e) {
                                            echo 'Caught exception: ', $e->getMessage(), "\n";
                                            die();
                                            
                                    }
    
                            }
                    } else {
                            if (!empty($id)) {
                                    header("Location:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=" . Configure::read('GOOGLE_OAUTH_CLIENT_ID') . "&redirect_uri=" . Configure::read('GOOGLE_OAUTH_REDIRECT_URI') . "&access_type=offline&approval_prompt=force&state=" . $id . "&scope=https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/analytics");
                                    exit;
                            }
                    }
    }
    
    //function to fetch linked account list
    function adwordsLinkAnalytics($analyticsAuth) {
                    $this->autoRender = false;
    
            
                    try {
                            $adWordsLinks = $analyticsAuth->management_webPropertyAdWordsLinks
                                    ->listManagementwebPropertyAdWordsLinks('123456', 'UA-123456-1');
    
                    } catch (apiServiceException $e) {
                            print 'There was an Analytics API service error '
                            . $e->getCode() . ':+' . $e->getMessage();
                            exit;
    
                    } catch (apiException $e) {
                            print 'There was a general API error '
                            . $e->getCode() . ':-' . $e->getMessage();
                            exit;
                    }
                    pr($adWordsLinks);
                    exit;
                    
    }
    

    您在Google Analytics中缺少管理实体的范围,请查看此

    请使用“更新您的范围”https://www.googleapis.com/auth/analytics.edit“

    我建议的更新:

    参考Url:


    您在Google Analytics中缺少管理实体的范围,请看这个

    请使用“更新您的范围”https://www.googleapis.com/auth/analytics.edit“

    我建议的更新:

    参考Url:


    没有
    https://www.googleapis.com/auth/adwords
    范围。再次检查第页。请始终将错误消息的详细信息包含为文本,而不是图像。图像不仅使任何有视觉障碍或小屏幕的人都很难帮助你,还意味着没有人会在搜索相同的错误消息时看到你的问题。没有
    https://www.googleapis.com/auth/adwords
    范围。再次检查第页。请始终将错误消息的详细信息包含为文本,而不是图像。图像不仅使任何有视觉障碍或小屏幕的人都很难帮助你,还意味着没有人会在搜索相同的错误消息时看到你的问题。
    function googleAuth(){
    
            if (!empty($code)) {
    
                    --------------
                    ---- Your existing script ----
                    --------------
            
            } else {
                    if (!empty($id)) {
                            header("Location:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=" . Configure::read('GOOGLE_OAUTH_CLIENT_ID') . "&redirect_uri=" . Configure::read('GOOGLE_OAUTH_REDIRECT_URI') . "&access_type=offline&approval_prompt=force&state=" . $id . "&scope=https://www.googleapis.com/auth/adwords%20https://www.googleapis.com/auth/analytics%20https://www.googleapis.com/auth/analytics.edit");
                            exit;
                    }
            }
    }