error: Undefined property while fetching data from Google analytics

error: Undefined property while fetching data from Google analytics

我在从 google analytics 获取数据(具体来说是配置文件列表)时遇到问题。 client_id、secret 等凭证已到位,它确实允许用户成功登录,但我被困在下一步,我需要获取配置文件列表(网站列表)。我确实按照 Google Views (Profiles): list 浏览了官方文档,但是当我尝试时,我得到一个错误:

Undefined property: App\Http\Controllers\UserController::$analytics

详细错误:

in UserController.php line 84 at HandleExceptions->handleError('8', 'Undefined property: App\Http\Controllers\UserController::$analytics', 'C:\xampp\htdocs\Laravel Projects\testApp\app\Http\Controllers\UserController.php', '84', array('request' => object(Request), 'google_redirect_url' => 'http://localhost:8000/glogin', 'gClient' => object(Google_Client), 'google_oauthV2' => object(Google_Service_Oauth2), 'guser' => null, 'user' => object(User), 'token' => array('access_token' => 'TOKEN GOES HERE', 'token_type' => 'Bearer', 'expires_in' => '3600', 'id_token' => 'ID_TOKEN GOES HERE', 'created' => 'CREATED DATA GOES HERE'))) in UserController.php line 84

我知道当我尝试获取数据时出现错误,但我真的不明白我应该怎么做。有人知道吗?请帮忙!

这是 Controller

 class UserController extends Controller
{
  public function googleLogin(Request $request)  {
    $google_redirect_url = route('glogin');
    $gClient = new \Google_Client();
    $gClient->setApplicationName(config('services.google.app_name'));
    $gClient->setClientId(config('services.google.client_id'));
    $gClient->setClientSecret(config('services.google.client_secret'));
    $gClient->setRedirectUri($google_redirect_url);
    $gClient->setDeveloperKey(config('services.google.api_key'));

    $gClient->addScope(\Google_Service_Analytics::ANALYTICS_READONLY);
    $gClient->addScope("email");
    $gClient->addScope("profile");
    $gClient->setAccessType("offline");
    $google_oauthV2 = new \Google_Service_Oauth2($gClient);
    if ($request->get('code')){
        $gClient->authenticate($request->get('code'));
        $request->session()->put('token', $gClient->getAccessToken());
    }
    if ($request->session()->get('token'))
    {
        $gClient->setAccessToken($request->session()->get('token'));
    }
    if ($gClient->getAccessToken())
    {
        //For logged in user, get details from google using access token
        $guser = $google_oauthV2->userinfo->get();

        $request->session()->put('name', $guser['name']);
        if ($user =User::where('email',$guser['email'])->first())
        {


        }else{
            //register your user with response data

            return User::create([
                'name' => $guser->name,
                'email' => $guser->email,
            ]);
        }

        //LINE NO 84 is below:
        $profiles = $this->analytics->management_profiles
            ->listManagementProfiles();
        $accounts = $accountsObject->getItems();

        return $accounts;
        //return redirect()->route('user.glist');
    } else
    {
        //For Guest user, get google login url
     }
   }
}

您正在调用 $this->analytics,但您没有为 UserController class 定义 属性 analytics。不确定父 class Controller 中定义了什么,但我很确定它与 Analytics 服务对象无关。

您需要实例化 Google_Service_Analytics 对象。

// Create an authorized analytics service object.
$analytics = new Google_Service_Analytics($gclient);

详情见Hello Analytics guide