0

我正在尝试通过调用来获取谷歌报告活动https://www.googleapis.com/admin/reports/v1/activity/users/all/applications/meet

我创建了一个服务帐户,我必须使用生成的private key(json 文件)作为access token.

我的代码是:

String PROTECTED_RESOURCE_URL = "https://www.googleapis.com/admin/reports/v1/activity/users/all/applications/meet?eventName=call_ended&maxResults=10&access_token=";
        String graph = "";
        try
        {
            JSONParser parser = new JSONParser();
            JSONObject data = (JSONObject) parser.parse(
                  new FileReader("C:/Users/Administrateur/Desktop/GoogleApis/Interoperability-googleApis/target/classes/my-first-project-274515-361633451f1c.json"));//path to the JSON file.

            String json_private_key = data.toJSONString();

            URL urUserInfo = new URL(PROTECTED_RESOURCE_URL + json_private_key);
            HttpURLConnection connObtainUserInfo = (HttpURLConnection) urUserInfo.openConnection();
            if (connObtainUserInfo.getResponseCode() == HttpURLConnection.HTTP_OK)
            {
                StringBuilder sbLines = new StringBuilder("");

                BufferedReader reader = new BufferedReader(new InputStreamReader(connObtainUserInfo.getInputStream(), "utf-8"));
                String strLine = "";
                while ((strLine = reader.readLine()) != null)
                {
                    sbLines.append(strLine);
                }
                graph = sbLines.toString();
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

        System.out.println("--------------- Result: " + graph);

但我得到了空值。

你能告诉我我想念什么吗?非常感谢。

4

1 回答 1

0

访问令牌不是您的请求 URL 的一部分。您可以在此处阅读有关 OAuth2 协议及其工作原理的信息。

但是,Google 构建了一个 API,使您能够对请求进行身份验证,而无需担心底层的 OAuth2 过程。您应该使用 Java Google Reports API 来访问活动。在这里您可以找到 Java 快速入门,它将帮助您首次设置 Java 应用程序。

这里是您尝试使用 Google Reports API 进行的 Java 翻译:

Reports service = new Reports.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                  .setApplicationName(APPLICATION_NAME)
                  .build();

String userKey = "all";
String applicationName = "meet";
String eventName = "call_ended";
Activities result = service.activities().list(userKey, applicationName)
                    .setEventName(eventName)
                    .setMaxResults(10)
                    .execute();

编辑:

请务必使用最新版本的 Java API 包。您可以在此处找到 Java API 文档:https ://developers.google.com/resources/api-libraries/documentation/admin/reports_v1/java/latest/

如果您使用 Gradle,请确保在 dependencies 参数中有这一行。

dependencies {
   ...
   compile 'com.google.apis:google-api-services-admin-reports:reports_v1-rev89-1.25.0'
}

参考

OAuth2

谷歌报告 API

于 2020-04-20T10:42:16.770 回答