谷歌分析特定帐户的无效凭据

时间:2012-12-17 21:53:04

标签: php google-analytics oauth-2.0 google-analytics-api

您好我正在使用其他帐户进行Google分析并返回错误:

GDataauthErrorAuthorizationInvalid Credentials

虽然我以前的帐户工作正常。可能是因为我错过了注册过程中的一些事情,任何人都可以给我一个详细的步骤,使用api_key for google analytics v2.4进行oauth 2.0的注册工作

谢谢

//returns session token for multiple calls to API
function get_session_token($onetimetoken) {
    $output = call_api($onetimetoken, "https://www.google.com/accounts/AuthSubSessionToken");

    if(preg_match("/Token=(.*)/", $output, $matches)) {
        $sessionToken = $matches[1];
    } else {
        echo "Error authenticating with Google.";
        exit;
    }

    return $sessionToken;
}

//gets the data
function call_api($sessionToken,$url){
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    if ($_SESSION['authSub']==true){
        $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $sessionToken);
    } else {
        $curlheader[0] = "Authorization: GoogleLogin auth=" . $sessionToken;
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

    $response = curl_exec($curl);
    curl_close($curl);

    return $response;       
}

$requrlvisits = sprintf("https://www.googleapis.com/analytics/v2.4/data?ids=ga:%s&dimensions=ga:date&metrics=ga:visits,ga:pageviews,ga:bounces,ga:timeOnSite&start-date=%s&end-date=%s&sort=ga:date&key=%s",$get_profid[9],$date1,$date2,$api_key);
                            // echo $requrlvisits;

                            $visitsxml = call_api($_SESSION['sessionToken'],$requrlvisits);             
                            $visits = parse_data($visitsxml);

                            print_r($visitsxml);

0 个答案:

没有答案