我正在尝试通过致电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);
但我的值为空。
您能告诉我我想念的吗? 非常感谢。
答案 0 :(得分:0)
访问令牌不是您请求URL的一部分。您可以阅读here有关OAuth2协议及其工作原理的信息。
但是,Google构建了一个API,使您可以对请求进行身份验证,而不必担心基础OAuth2流程。 您应该使用Java Google Reports API访问活动。 Here您可以找到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'
}