Skip to content

Commit

Permalink
Add support for logging in and out by command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Rogers committed Jul 28, 2017
1 parent cd35939 commit 9d73bd6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ these steps to setup the project directory.

```
./gradlew idea
./gradlew genIntellijRuns
```
2. Open the project folder with Intellij, and import the Gradle project
when prompted.
Expand Down Expand Up @@ -83,6 +82,20 @@ followed by *Other* for the Application type.
4. Download the json credentials by clicking the following icon for your
project: ![Download json](download_json.png)

5. Run the YouTube Chat jar from a terminal to obtain authorization credentials,
pasting the client ID JSON when prompted:

```
java -jar ytchat-1.0.3.jar login
```

If you want to clear your credentials or sign in as a different user, run logout:

```
java -jar ytchat-1.0.3.jar logout
```

Credentials are be saved to ~/.oauth-credentials.

## Configuration

Expand Down
3 changes: 3 additions & 0 deletions dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ task saveDependencies << {
}

jar {
manifest {
attributes 'Main-Class': 'com.google.youtube.gaming.chat.Main'
}
from configurations.embed.collect {
exclude 'META-INF/LICENSE*'
exclude 'META-INF/NOTICE*'
Expand Down
2 changes: 1 addition & 1 deletion setup.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ task updateMdk(dependsOn: 'stageMdk') {
String text = buildGradle.getText('UTF-8')
text = text.replaceAll('com.yourname.modid', 'com.google.youtube.ytchat')
text = text.replaceAll('modid', 'ytchat')
text = text.replaceAll('1\\.0', '1.0.2')
text = text.replaceAll('1\\.0', '1.0.3')
text = text.replace('repositories {', 'repositories {\n mavenCentral()');
text = text.replaceFirst(/\ndependencies\s?\{[^\}]+\}/, dependencyText);
buildGradle.write(text, 'UTF-8')
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/google/youtube/gaming/chat/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import org.apache.commons.io.FileUtils;

/**
* Contains methods for authorizing a user and caching credentials.
Expand Down Expand Up @@ -84,10 +81,24 @@ public static Credential authorize(
public static void clearCredentials() throws IOException {
File directory = new File(getCredentialsDirectory());
if (directory.exists()) {
FileUtils.deleteDirectory(directory);
deleteDirectory(directory);
}
}

private static boolean deleteDirectory(File path) {
if (path.exists()) {
File[] files = path.listFiles();
for(int i=0; i < files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return path.delete();
}

private static String getCredentialsDirectory() {
return System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/google/youtube/gaming/chat/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.youtube.gaming.chat;

import com.google.api.services.youtube.YouTubeScopes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
showUsage();
return;
}

switch (args[0]) {
case "login":
System.out.print("Paste the client ID JSON from the Google API console:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String clientSecret = br.readLine();
List<String> scopes = new ArrayList<String>();
scopes.add(YouTubeScopes.YOUTUBE_FORCE_SSL);
scopes.add(YouTubeScopes.YOUTUBE);
Auth.authorize(scopes, clientSecret, YouTubeChat.MODID);
break;
case "logout":
Auth.clearCredentials();
break;
default:
showUsage();
}
}

private static void showUsage() {
System.err.println("Supported arguments: login | logout");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class YouTubeChat {
public static final String MODID = "ytchat";
public static final String APPNAME = "YouTube Chat";
public static final String VERSION = "1.0.2";
public static final String VERSION = "1.0.3";
public static final String GUI_FACTORY =
"com.google.youtube.gaming.chat.YouTubeConfigurationGuiFactory";

Expand Down

0 comments on commit 9d73bd6

Please sign in to comment.