【问题标题】:error: Undefined property while fetching data from Google analytics错误:从 Google 分析获取数据时未定义的属性
【发布时间】:2016-07-22 08:53:35
【问题描述】:

我在从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
     }
   }
}

【问题讨论】:

    标签: php laravel google-analytics google-analytics-api


    【解决方案1】:

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

    您需要实例化Google_Service_Analytics 对象。

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

    详情请参阅Hello Analytics guide

    【讨论】:

    • 哦,是的,我错过了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多