laravel - 基于个人资料获取Google AnalyticsAPI数据

时间:2017-02-07 19:37:31

标签: php laravel google-analytics

我正在尝试构建一个应用程序,用户将使用他们的分析帐户进行身份验证,并根据他们选择的分析配置文件,将获取数据并以折线图显示。

到目前为止,我已经成功进行了身份验证(感谢我之前在stackoverflow上找到的一个问题)并在下拉列表中列出了分析配置文件,用户可以在其中选择任何一个。

现在我一直在寻找一种方法来获取所选配置文件的正确数据。

这两种方法是我用于身份验证部分的方法:

public function redirectToProvider()
{

    $client = new Google_Client();
    $client->setAuthConfig(storage_path('app/laravel-google-analytics/service-account-credentials.json'));
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/public/authhh/google/callback');

    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
    $client->addScope("email");
    $client->addScope("profile");
    $client->setAccessType("offline");

    $auth_url = $client->createAuthUrl();
    return redirect($auth_url);

}


public function handleProviderCallback()
{


    // Handle authorization flow from the server.
    if (! isset($_GET['code'])) {
        return redirect('auth/google');
    } else {


        // Authenticate the client, and get required informations.
        $client = new Google_Client();
        $client->setAuthConfig(storage_path('app/laravel-google-analytics/service-account-credentials.json'));
        $client->authenticate($_GET['code']);

        // Store the tokens in the session.
        Session::put('token', $client->getAccessToken());

        File::put(storage_path('app/laravel-google-analytics/google_token.json'),json_encode($client->getAccessToken()));

        $service = new Google_Service_Oauth2($client);
        $userInfo = $service->userinfo->get();

        $user = User::where('googleID', $userInfo->id)->first();

        //dd($client->getRefreshToken());

        // If no match, update the current user.
        if(!$user) {
            $user = User::find(Auth::user()->id);
            $user->google_name = $userInfo->name;
            $user->googleID = $userInfo->id;
            $user->google_email = $userInfo->email;
            $user->refreshToken = $client->getRefreshToken();
            $user->code = $_GET['code'];
            $user->update();
        }

        Auth::login($user);
        return redirect('/');
    }

}

然后,为了列出我使用的经过身份验证的分析帐户下的配置文件:

public function viewUserSettings(){

    $accountsObjects = [];

    if(Auth::user()->googleID) {
        $this->client = $this->AuthenticateCurrentClient();
        $this->analytics = new Google_Service_Analytics($this->client);

        $accountsObjects = $this->GetAccounts();
    }

    return view('user.user_settings', compact('accountsObjects'));

}

这两个是AuthenticateCurrentClient()和GetAccounts()方法:

public function AuthenticateCurrentClient(){

    $this->user = Auth::user();
    $token = Session::get('token');

    if(empty($token)){

        $token = json_decode(File::get(storage_path('app/laravel-google-analytics/google_token.json')),true);

        Session::put('token', $token);
    }

    $client = new Google_Client();

    $client->setAuthConfig(storage_path('app/laravel-google-analytics/service-account-credentials.json'));
    $client->setAccessToken($token);
    $client->authenticate($this->user->code);
    return $client;

}

public function GetAccounts(){

    try {
        $accountsObject = $this->analytics->management_accounts->listManagementAccounts();
        $accounts = $accountsObject->getItems();

        return $accounts;

    } catch (apiServiceException $e) {
        print 'There was an Analytics API service error '
            . $e->getCode() . ':' . $e->getMessage();

    } catch (apiException $e) {
        print 'There was a general API error '
            . $e->getCode() . ':' . $e->getMessage();
    }

}

在我的user_settings视图中,我只是有一个foreach循环,使用配置文件填充下拉列表选择:

<select name="analytics-profile" class="form-control">

                        @foreach($accountsObjects as $key=>$accountObject)
                            <option id="{{$accountObject->id}}">{{$accountObject->name}}</option>
                        @endforeach

                    </select>

我通过这个api的作曲家安装的软件包是:“google / apiclient”:“^ 2.0”。

非常感谢任何帮助,谢谢。

0 个答案:

没有答案