gpt4 book ai didi

java - 无法从 Google Analytics 获取数据(401 未经授权)

转载 作者:搜寻专家 更新时间:2023-11-01 03:11:44 26 4
gpt4 key购买 nike

我尝试实现基本的数据提要示例,但我得到的只是:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized.

但是当我使用 http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
它适用于我的 Google Analytics(分析)帐户。

看来我已经按照 OAuth2 说明完成了所有操作:创建客户端 ID 和客户端密码,启用 Analytcs API。

我还尝试使用 Analytics Management API 获取我的个人资料和帐户,但我遇到了同样的错误。我做错了什么?

这是我的代码(当然我用实际的 CLIENT_ID、CLIENT_SECRET 和 TABLE_ID 运行这段代码):

public class AnalyticsTest {

private static final String CLIENT_ID = "MY_CLIENT_ID";
private static final String CLIENT_SECRET = "MY_CLIENT_SECRET";
private static final String REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob";
private static final String APPLICATION_NAME = "test";
private static final String SCOPE = "https://www.googleapis.com/auth/analytics";
// TRIED AS WELL private static final String SCOPE = "https://www.google.com/analytics/feeds";
private static final String TABLE_ID = "MY_TABLE_ID";

public static void main(String[] args) throws IOException {
NetHttpTransport netHttpTransport = new NetHttpTransport();
JacksonFactory jacksonFactory = new JacksonFactory();
// Generate the URL to send the user to grant access.
String authorizationUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID, REDIRECT_URL, SCOPE).build();

// Direct user to the authorization URI.
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);

// Get authorization code from user.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String authorizationCode = null;
authorizationCode = in.readLine();

// Use the authorization code to get an access token and a refresh
// token.
AccessTokenResponse response = null;
try {
response = new GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant(netHttpTransport, jacksonFactory, CLIENT_ID, CLIENT_SECRET, authorizationCode, REDIRECT_URL).execute();
} catch (IOException ioe) {
ioe.printStackTrace();
}

// Use the access and refresh tokens to get a new
// GoogleAccessProtectedResource.
GoogleAccessProtectedResource googleAccessProtectedResource = new GoogleAccessProtectedResource(response.accessToken, netHttpTransport, jacksonFactory, CLIENT_ID, CLIENT_SECRET, response.refreshToken);

Analytics analytics = Analytics
.builder(netHttpTransport, jacksonFactory)
.setHttpRequestInitializer(googleAccessProtectedResource)
.setApplicationName(APPLICATION_NAME).build();
//System.out.println(analytics.management().accounts().list().execute());

Get apiQuery = analytics.data().ga().get(TABLE_ID, // "ga:" + Profile
// Id.
"2011-09-01", // Start date.
"2011-12-23", // End date.
"ga:visits"); // Metrics.

try {
GaData gaData = apiQuery.execute();
// Success. Do something cool!

} catch (GoogleJsonResponseException e) {
// Catch API specific errors.
e.printStackTrace();

} catch (IOException e) {
// Catch general parsing errors.
e.printStackTrace();
}

}

这是堆栈跟踪:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:138)
at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:123)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)
at com.google.api.services.analytics.Analytics$Data$Ga$Get.execute(Analytics.java:1335)
at voc.AnalyticsTest.main(AnalyticsTest.java:76)

最佳答案

尝试改变你的范围:

private static final String SCOPE = "https://www.googleapis.com/auth/analytics";

为此:

private static final String SCOPE = "https://www.googleapis.com/auth/analytics.readonly";

关于java - 无法从 Google Analytics 获取数据(401 未经授权),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8627503/

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