1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.publication.youtube.auth;
24
25 import org.opencastproject.util.data.Collections;
26
27 import org.apache.commons.lang3.builder.ToStringBuilder;
28 import org.json.simple.JSONObject;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
31
32 import java.io.File;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.util.List;
36
37
38
39
40
41 public final class ClientCredentials {
42
43 private String clientId;
44 private String credentialDatastore;
45 private String dataStoreDirectory;
46 private File clientSecrets;
47
48 public String getClientId() {
49 return clientId;
50 }
51
52 public String getCredentialDatastore() {
53 return credentialDatastore;
54 }
55
56 public String getDataStoreDirectory() {
57 return dataStoreDirectory;
58 }
59
60 public List<String> getScopes() {
61 return Collections.list("https://www.googleapis.com/auth/youtube",
62 "https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube.readonly");
63 }
64
65 public File getClientSecrets() {
66 return clientSecrets;
67 }
68
69 public void setClientSecrets(final File clientSecrets) throws IOException, ParseException {
70 this.clientSecrets = clientSecrets;
71 this.clientId = getValueFromArray(clientSecrets);
72 }
73
74 public void setCredentialDatastore(final String credentialDatastore) {
75 this.credentialDatastore = credentialDatastore;
76 }
77
78 public void setDataStoreDirectory(final String dataStoreDirectory) {
79 this.dataStoreDirectory = dataStoreDirectory;
80 }
81
82 @Override
83 public String toString() {
84 return ToStringBuilder.reflectionToString(this);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99 private String getValueFromArray(final File file) throws IOException, ParseException, IllegalArgumentException {
100 final JSONParser parser = new JSONParser();
101 final FileReader reader = new FileReader(file);
102 final JSONObject jsonObject = (JSONObject) parser.parse(reader);
103
104 if (!jsonObject.containsKey("installed")) {
105 throw new IllegalArgumentException("client_secret file does not contain \"installed\" as a top level key. Did you"
106 + "set the type for your 'OAuth 2.0-Client-ID' correctly?");
107 }
108 final JSONObject array = (JSONObject) jsonObject.get("installed");
109
110 if (!array.containsKey("client_id")) {
111 throw new IllegalArgumentException("client_secret file does not contain \"client_id\".");
112 }
113 return (String) array.get("client_id");
114 }
115
116 }