gpt4 book ai didi

php - 错误: Undefined property while fetching data from Google analytics

转载 作者:行者123 更新时间:2023-12-01 19:44:28 24 4
gpt4 key购买 nike

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

未定义的属性:App\Http\Controllers\UserController::$analytics

详细错误:

在 UserController.php 第 84 行
在 HandleExceptions->handleError('8', '未定义的属性: App\Http\Controllers\UserController::$analytics', 'C:\xampp\htdocs\Laravel Projects\testApp\app\Http\Controllers\UserController.php' , '84', array('request' => 对象(请求), 'google_redirect_url' => 'http://localhost:8000/glogin', 'gClient' => 对象(Google_Client), 'google_oauthV2' => 对象(Google_Service_Oauth2), 'guser' => null, 'user' => 对象(用户), 'token' => array('access_token' => ' token 在这里', 'token_type' => 'Bearer', 'expires_in ' => '3600', 'id_token' => 'ID_TOKEN 去这里', 'created' => '创建数据去这里'))) 在 UserController.php 第 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 类定义属性 analytics。不确定父类 Controller 中定义了什么,但我很确定它与 Analytics 服务对象无关。

您需要实例化 Google_Service_Analytics 对象。

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

请参阅Hello Analytics guide了解详情。

关于php - 错误: Undefined property while fetching data from Google analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com