java - 谷歌分析 403 : Forbidden with p12 key in Java

标签 java google-analytics google-analytics-api google-api-java-client

我正在尝试通过 API 将用户添加到 Google Analytics(分析)帐户。 我有以下代码:

private static final String APPLICATION_NAME = "MyNameProject";

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

private static final JsonFactory JSON_FACTORY = new JacksonFactory();

private static final String SERVICE_ACCOUNT_EMAIL = "XXXXXXX@developer.gserviceaccount.com";

private static Credential authorize() throws Exception {
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(
                    Collections.singleton(AnalyticsScopes.ANALYTICS_MANAGE_USERS))
            .setServiceAccountPrivateKeyFromP12File(new File("/bla/MyKey.p12"))
            .build();
    credential.refreshToken();
    return credential;
}

public static void main(String[] args) {
    try {
        // Authorize this application to access the user's data.
        Credential credential = authorize();

        // Create an authorized Maps Engine client with the credential.
        Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();

        UserRef userRef = new UserRef();
        userRef.setEmail("usertoadd@gmail.com");//add mail


        // Construct the permissions object.
        Permissions permissions = new Permissions();
        List local = Arrays.asList("EDIT", "MANAGE_USERS");
        permissions.setLocal(local);

        // Construct the body of the request
        EntityUserLink body = new EntityUserLink();
        body.setPermissions(permissions);
        body.setUserRef(userRef);

        analytics.management().profileUserLinks().insert("66666666", "UA-55555555-1",
                "888888888", body).execute();

    } catch (GoogleJsonResponseException e) {
        System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                + e.getDetails().getMessage());
        e.printStackTrace();

    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

我遇到以下错误:

There was a service error: 403 : User does not have sufficient permissions for this account.
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

JSON: { “代码”:403, “错误”:[ { “域”:“全局”, "message": "用户对此帐户没有足够的权限。", "原因": "权限不足" }], "message": "用户对此帐户没有足够的权限。" }

我不知道该怎么办。 有什么帮助吗? 谢谢!

最佳答案

Matt帮助我解决了这个问题并且成功了。 解决办法是先将“XXX...@developer.gserviceaccount.com”添加到可以管理用户的授权用户列表中。 服务帐户的行为类似于单独的授权用户,可以在帐户、属性或 View 级别添加。

这里是帖子:https://groups.google.com/forum/#!topic/google-analytics-management-api/hM1cg-YMdrA

再见!

关于java - 谷歌分析 403 : Forbidden with p12 key in Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29525701/

相关文章:

java - org.apache.solr.common.SolrException : Bad Request Bad Request request: http://localhost:8080/solr/update? wt=javabin&version=2

google-analytics - 对于同一个网站拥有两个帐户,我的代码是否正确?

google-analytics - 记录电子邮件作为事件打开

google-analytics - 通过分析获取删除的adwords广告统计信息

javascript - 使用 Google Analytics 中的自定义维度和指标收集调查数据

java - 使用 Spring ACL 3 保护域对象

Java SOAPConnection 调用 Https ASMX 服务

java - 更改代码后如何运行和部署 Spring Boot 应用程序

android - 尝试为 Android 应用安装谷歌分析 - 启动方法已弃用

javascript - 谷歌分析嵌入 API : How to retireve access token?