我遇到了从google analytics
获取数据(特定于个人资料列表)的问题。像client_id
,秘密等凭证到位,它确实允许用户成功登录,但我陷入下一步,我需要获取配置文件列表(网站列表)。我确实按照 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
}
}
}
答案 0 :(得分:1)
您正在调用$this->analytics
,但您无法为analytics
类定义属性UserController
。不确定父类Controller
中定义了什么,但我非常确定它与Analytics服务对象无关。
您需要实例化Google_Service_Analytics
对象。
// Create an authorized analytics service object.
$analytics = new Google_Service_Analytics($gclient);
有关详细信息,请参阅Hello Analytics guide。