├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── cafe
│ │ └── adriel
│ │ └── androidoauth
│ │ └── example
│ │ ├── Credentials.java
│ │ └── MainActivity.java
│ └── res
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lib
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── cafe
│ │ └── adriel
│ │ └── androidoauth
│ │ ├── callback
│ │ ├── OnGetCodeCallback.java
│ │ ├── OnLoginCallback.java
│ │ └── OnLogoutCallback.java
│ │ ├── model
│ │ └── SocialUser.java
│ │ ├── oauth
│ │ ├── BaseOAuth.java
│ │ ├── FacebookOAuth.java
│ │ ├── GoogleOAuth.java
│ │ ├── ILoginOAuth.java
│ │ ├── ILogoutOAuth.java
│ │ └── OAuthProvider.java
│ │ └── view
│ │ ├── ConsentDialog.java
│ │ └── KeyboardWebView.java
│ └── res
│ └── values
│ └── strings.xml
├── logo.png
├── screenshots
├── facebook-auth.jpg
├── facebook-consent.jpg
├── google-auth.jpg
└── google-consent.jpg
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | /projectFilesBackup
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | AndroidGoogleOAuth
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://android-arsenal.com/details/1/3837) [](https://jitpack.io/#adrielcafe/AndroidOAuth)
2 |
3 | 
4 |
5 | > A simple way to authenticate with **Google** and **Facebook** using **OAuth 2.0** in Android
6 |
7 | ## How To Use
8 |
9 | ### Google
10 |
11 |  
12 |
13 | #### Login
14 | ```java
15 | // Use a Web credential instead of Android credential
16 | GoogleOAuth.login(this)
17 | .setClientId(Credentials.GOOGLE_CLIENT_ID)
18 | .setClientSecret(Credentials.GOOGLE_CLIENT_SECRET)
19 | .setAdditionalScopes("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/user.birthday.read")
20 | .setRedirectUri(Credentials.GOOGLE_REDIRECT_URI)
21 | .setCallback(new OnLoginCallback() {
22 | @Override
23 | public void onSuccess(String token, SocialUser user) {
24 | Log.d("Google Token", token);
25 | Log.d("Google User", user+"");
26 | }
27 | @Override
28 | public void onError(Exception error) {
29 | error.printStackTrace();
30 | }
31 | })
32 | .init();
33 | ```
34 |
35 | #### Logout ([revoke token](https://developers.google.com/identity/protocols/OAuth2WebServer#tokenrevoke))
36 | ```java
37 | GoogleOAuth.logout(this)
38 | .setToken(currentToken)
39 | .setCallback(new OnLogoutCallback() {
40 | @Override
41 | public void onSuccess() {
42 |
43 | }
44 | @Override
45 | public void onError(Exception error) {
46 |
47 | }
48 | })
49 | .init();
50 | ```
51 |
52 | ### Facebook
53 |
54 |  
55 |
56 | #### Login
57 | ```java
58 | // No need to configure Android section on Facebook app
59 | FacebookOAuth.login(this)
60 | .setClientId(Credentials.FACEBOOK_APP_ID)
61 | .setClientSecret(Credentials.FACEBOOK_APP_SECRET)
62 | .setAdditionalScopes("user_friends user_birthday")
63 | .setRedirectUri(Credentials.FACEBOOK_REDIRECT_URI)
64 | .setCallback(new OnLoginCallback() {
65 | @Override
66 | public void onSuccess(String token, SocialUser user) {
67 | Log.d("Facebook Token", token);
68 | Log.d("Facebook User", user+"");
69 | }
70 | @Override
71 | public void onError(Exception error) {
72 | error.printStackTrace();
73 | }
74 | })
75 | .init();
76 | ```
77 |
78 | #### Logout ([revoke token](https://developers.facebook.com/docs/facebook-login/permissions/requesting-and-revoking#revokelogin))
79 | ```java
80 | FacebookOAuth.logout(this)
81 | .setToken(currentToken)
82 | .setCallback(new OnLogoutCallback() {
83 | @Override
84 | public void onSuccess() {
85 |
86 | }
87 | @Override
88 | public void onError(Exception error) {
89 |
90 | }
91 | })
92 | .init();
93 | ```
94 |
95 |
96 | ## Import to your project
97 | Put this into your `app/build.gradle`:
98 | ```
99 | repositories {
100 | maven {
101 | url "https://jitpack.io"
102 | }
103 | }
104 |
105 | dependencies {
106 | compile 'com.github.adrielcafe:AndroidOAuth:1.1.5'
107 | }
108 | ```
109 |
110 | ## TODO
111 | - [ ] Twitter support
112 | - [X] Get `name`, `email`, `profileUrl`, `coverUrl` and `birthday` from authenticated user
113 | - [X] `logout()` method to revoke token
114 | - [X] `setAdditionalScopes()` method to add more scopes from [Google](https://developers.google.com/identity/protocols/googlescopes) and [Facebook](https://developers.facebook.com/docs/facebook-login/permissions)
115 |
116 | ## Dependencies
117 | * [ScribeJava](https://github.com/scribejava/scribejava)
118 | * [HttpAgent](https://github.com/studioidan/HttpAgent)
119 |
120 | ## License
121 | ```
122 | The MIT License (MIT)
123 |
124 | Copyright (c) 2016 Adriel Café
125 |
126 | Permission is hereby granted, free of charge, to any person obtaining a copy
127 | of this software and associated documentation files (the "Software"), to deal
128 | in the Software without restriction, including without limitation the rights
129 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
130 | copies of the Software, and to permit persons to whom the Software is
131 | furnished to do so, subject to the following conditions:
132 |
133 | The above copyright notice and this permission notice shall be included in
134 | all copies or substantial portions of the Software.
135 |
136 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
137 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
138 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
139 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
140 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
141 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
142 | THE SOFTWARE.
143 | ```
144 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "24.0.2"
6 |
7 | defaultConfig {
8 | applicationId "cafe.adriel.androidoauth"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile 'com.android.support:appcompat-v7:24.2.1'
24 | compile project(':lib')
25 | }
26 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/adrielcafe/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/cafe/adriel/androidoauth/example/Credentials.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.example;
2 |
3 | public class Credentials {
4 | public static final String GOOGLE_CLIENT_ID = "940891630461-uh38ng1a01tfkmu926g5f4el56h9hock.apps.googleusercontent.com";
5 | public static final String GOOGLE_CLIENT_SECRET = "oxvq5lNSJvdM_zwZgc_appQV";
6 | public static final String GOOGLE_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob:auto";
7 |
8 | public static final String FACEBOOK_APP_ID = "1540807289473122";
9 | public static final String FACEBOOK_APP_SECRET = "236e282277e8ee3150c447cf0307bcb3";
10 | public static final String FACEBOOK_REDIRECT_URI = "http://demo.xarx.rocks/";
11 | }
--------------------------------------------------------------------------------
/app/src/main/java/cafe/adriel/androidoauth/example/MainActivity.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.example;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 | import android.widget.TextView;
7 | import android.widget.Toast;
8 |
9 | import cafe.adriel.androidoauth.callback.OnLoginCallback;
10 | import cafe.adriel.androidoauth.callback.OnLogoutCallback;
11 | import cafe.adriel.androidoauth.model.SocialUser;
12 | import cafe.adriel.androidoauth.oauth.FacebookOAuth;
13 | import cafe.adriel.androidoauth.oauth.GoogleOAuth;
14 | import cafe.adriel.androidoauth.oauth.OAuthProvider;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 | private OAuthProvider currentProvider;
18 | private String currentToken;
19 |
20 | private TextView tokenView;
21 | private TextView accountView;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_main);
27 |
28 | tokenView = (TextView) findViewById(R.id.token);
29 | accountView = (TextView) findViewById(R.id.account);
30 | }
31 |
32 | public void googleLogin(View v) {
33 | GoogleOAuth.login(this)
34 | .setClientId(Credentials.GOOGLE_CLIENT_ID)
35 | .setClientSecret(Credentials.GOOGLE_CLIENT_SECRET)
36 | .setAdditionalScopes("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/user.birthday.read")
37 | .setRedirectUri(Credentials.GOOGLE_REDIRECT_URI)
38 | .setCallback(new OnLoginCallback() {
39 | @Override
40 | public void onSuccess(String token, SocialUser user) {
41 | afterLogin(token, user);
42 | }
43 | @Override
44 | public void onError(Exception error) {
45 | error.printStackTrace();
46 | }
47 | })
48 | .init();
49 | }
50 |
51 | public void facebookLogin(View v) {
52 | FacebookOAuth.login(this)
53 | .setClientId(Credentials.FACEBOOK_APP_ID)
54 | .setClientSecret(Credentials.FACEBOOK_APP_SECRET)
55 | .setAdditionalScopes("user_friends user_birthday")
56 | .setRedirectUri(Credentials.FACEBOOK_REDIRECT_URI)
57 | .setCallback(new OnLoginCallback() {
58 | @Override
59 | public void onSuccess(String token, SocialUser user) {
60 | afterLogin(token, user);
61 | }
62 | @Override
63 | public void onError(Exception error) {
64 | error.printStackTrace();
65 | }
66 | })
67 | .init();
68 | }
69 |
70 | public void logout(View v) {
71 | if (currentToken != null) {
72 | OnLogoutCallback callback = new OnLogoutCallback() {
73 | @Override
74 | public void onSuccess() {
75 | Toast.makeText(MainActivity.this, "Logout Success!", Toast.LENGTH_SHORT).show();
76 | afterLogout();
77 | }
78 |
79 | @Override
80 | public void onError(Exception error) {
81 | Toast.makeText(MainActivity.this, "Logout Error: " + error.getMessage(),
82 | Toast.LENGTH_LONG).show();
83 | }
84 | };
85 | switch (currentProvider) {
86 | case GOOGLE:
87 | GoogleOAuth.logout(this)
88 | .setToken(currentToken)
89 | .setCallback(callback)
90 | .init();
91 | break;
92 | case FACEBOOK:
93 | FacebookOAuth.logout(this)
94 | .setToken(currentToken)
95 | .setCallback(callback)
96 | .init();
97 | break;
98 | }
99 | } else {
100 | Toast.makeText(this, "Token not found. Login first.", Toast.LENGTH_SHORT).show();
101 | }
102 | }
103 |
104 | private void afterLogin(String token, SocialUser user){
105 | currentToken = token;
106 | currentProvider = user.getProvider();
107 | tokenView.setText(currentProvider +" Token: \n" + token);
108 | accountView.setText(currentProvider +" User: \n" + user);
109 | }
110 |
111 | private void afterLogout(){
112 | tokenView.setText("");
113 | accountView.setText("");
114 | }
115 |
116 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
27 |
28 |
36 |
37 |
45 |
46 |
47 |
48 |
51 |
52 |
56 |
57 |
62 |
63 |
69 |
70 |
71 |
72 |
73 |
74 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidOAuth
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | }
5 | dependencies {
6 | classpath 'com.android.tools.build:gradle:2.2.1'
7 | }
8 | }
9 |
10 | allprojects {
11 | repositories {
12 | jcenter()
13 | }
14 | }
15 |
16 | task clean(type: Delete) {
17 | delete rootProject.buildDir
18 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Aug 25 16:09:46 BRT 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "24.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile 'com.github.scribejava:scribejava-apis:2.8.1'
23 | compile 'com.studioidan.httpagent:httpagent:1.0.3@aar'
24 | }
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/adrielcafe/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/callback/OnGetCodeCallback.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.callback;
2 |
3 | public interface OnGetCodeCallback {
4 |
5 | void onSuccess(String code);
6 |
7 | void onError(Exception error);
8 |
9 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/callback/OnLoginCallback.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.callback;
2 |
3 | import cafe.adriel.androidoauth.model.SocialUser;
4 |
5 | public interface OnLoginCallback {
6 |
7 | void onSuccess(String token, SocialUser user);
8 |
9 | void onError(Exception error);
10 |
11 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/callback/OnLogoutCallback.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.callback;
2 |
3 | public interface OnLogoutCallback {
4 |
5 | void onSuccess();
6 |
7 | void onError(Exception error);
8 |
9 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/model/SocialUser.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.model;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | import java.util.Date;
7 |
8 | import cafe.adriel.androidoauth.oauth.OAuthProvider;
9 |
10 | public class SocialUser implements Parcelable {
11 | private String id;
12 | private String name;
13 | private String email;
14 | private String pictureUrl;
15 | private String coverUrl;
16 | private Date birthday;
17 | private OAuthProvider provider;
18 |
19 | public SocialUser() {
20 |
21 | }
22 |
23 | public String getId() {
24 | return id;
25 | }
26 |
27 | public void setId(String id) {
28 | this.id = id;
29 | }
30 |
31 | public String getName() {
32 | return name;
33 | }
34 |
35 | public void setName(String name) {
36 | this.name = name;
37 | }
38 |
39 | public String getEmail() {
40 | return email;
41 | }
42 |
43 | public void setEmail(String email) {
44 | this.email = email;
45 | }
46 |
47 | public String getPictureUrl() {
48 | return pictureUrl;
49 | }
50 |
51 | public void setPictureUrl(String pictureUrl) {
52 | this.pictureUrl = pictureUrl;
53 | }
54 |
55 | public String getCoverUrl() {
56 | return coverUrl;
57 | }
58 |
59 | public void setCoverUrl(String coverUrl) {
60 | this.coverUrl = coverUrl;
61 | }
62 |
63 | public Date getBirthday() {
64 | return birthday;
65 | }
66 |
67 | public void setBirthday(Date birthday) {
68 | this.birthday = birthday;
69 | }
70 |
71 | public OAuthProvider getProvider() {
72 | return provider;
73 | }
74 |
75 | public void setProvider(OAuthProvider provider) {
76 | this.provider = provider;
77 | }
78 |
79 | @Override
80 | public String toString() {
81 | return String.format(
82 | "[id: %s, name: %s, email: %s, pictureUrl: %s, coverUrl: %s, birthday: %s, provider: %s]",
83 | id, name, email, pictureUrl, coverUrl, birthday, provider);
84 | }
85 |
86 | @Override
87 | public int describeContents() {
88 | return 0;
89 | }
90 |
91 | @Override
92 | public void writeToParcel(Parcel dest, int flags) {
93 | dest.writeString(this.id);
94 | dest.writeString(this.name);
95 | dest.writeString(this.email);
96 | dest.writeString(this.pictureUrl);
97 | dest.writeString(this.coverUrl);
98 | dest.writeLong(this.birthday != null ? this.birthday.getTime() : -1);
99 | dest.writeInt(this.provider == null ? -1 : this.provider.ordinal());
100 | }
101 |
102 | protected SocialUser(Parcel in) {
103 | this.id = in.readString();
104 | this.name = in.readString();
105 | this.email = in.readString();
106 | this.pictureUrl = in.readString();
107 | this.coverUrl = in.readString();
108 | long tmpBirthday = in.readLong();
109 | this.birthday = tmpBirthday == -1 ? null : new Date(tmpBirthday);
110 | int tmpProvider = in.readInt();
111 | this.provider = tmpProvider == -1 ? null : OAuthProvider.values()[tmpProvider];
112 | }
113 |
114 | public static final Creator CREATOR = new Creator() {
115 | @Override
116 | public SocialUser createFromParcel(Parcel source) {
117 | return new SocialUser(source);
118 | }
119 |
120 | @Override
121 | public SocialUser[] newArray(int size) {
122 | return new SocialUser[size];
123 | }
124 | };
125 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/BaseOAuth.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | import android.app.Activity;
4 | import android.os.AsyncTask;
5 |
6 | import com.github.scribejava.core.builder.ServiceBuilder;
7 | import com.github.scribejava.core.builder.api.DefaultApi20;
8 | import com.github.scribejava.core.model.OAuth2AccessToken;
9 | import com.github.scribejava.core.model.OAuthRequest;
10 | import com.github.scribejava.core.model.Response;
11 | import com.github.scribejava.core.model.Verb;
12 | import com.github.scribejava.core.oauth.OAuth20Service;
13 | import com.studioidan.httpagent.HttpAgent;
14 | import com.studioidan.httpagent.SuccessCallback;
15 |
16 | import java.util.UUID;
17 |
18 | import cafe.adriel.androidoauth.callback.OnGetCodeCallback;
19 | import cafe.adriel.androidoauth.callback.OnLoginCallback;
20 | import cafe.adriel.androidoauth.callback.OnLogoutCallback;
21 | import cafe.adriel.androidoauth.model.SocialUser;
22 | import cafe.adriel.androidoauth.view.ConsentDialog;
23 |
24 | public abstract class BaseOAuth {
25 | protected Activity activity;
26 | protected DefaultApi20 api;
27 | protected String getAccountUrl;
28 | protected String revokeTokenUrl;
29 | protected Verb revokeTokenVerb;
30 |
31 | protected BaseOAuth(Activity activity, DefaultApi20 api, String getAccountUrl,
32 | String revokeTokenUrl, Verb revokeTokenVerb) {
33 | this.activity = activity;
34 | this.api = api;
35 | this.getAccountUrl = getAccountUrl;
36 | this.revokeTokenUrl = revokeTokenUrl;
37 | this.revokeTokenVerb = revokeTokenVerb;
38 | }
39 |
40 | protected abstract SocialUser toAccount(String json);
41 |
42 | private SocialUser getAccount(OAuth20Service service, OAuth2AccessToken accessToken) {
43 | try {
44 | OAuthRequest request = new OAuthRequest(Verb.GET, getAccountUrl, service);
45 | service.signRequest(accessToken, request);
46 | Response response = request.send();
47 | return toAccount(response.getBody());
48 | } catch (Exception e) {
49 | return null;
50 | }
51 | }
52 |
53 | public static class LoginOAuth implements ILoginOAuth {
54 | protected BaseOAuth oAuth;
55 |
56 | protected String clientId;
57 | protected String clientSecret;
58 | protected String scopes;
59 | protected String redirectUri;
60 | protected OnLoginCallback callback;
61 |
62 | protected LoginOAuth(BaseOAuth oAuth, String scopes) {
63 | this.oAuth = oAuth;
64 | this.scopes = scopes;
65 | }
66 |
67 | @Override
68 | public LoginOAuth setClientId(String clientId) {
69 | this.clientId = clientId;
70 | return this;
71 | }
72 |
73 | @Override
74 | public LoginOAuth setClientSecret(String clientSecret) {
75 | this.clientSecret = clientSecret;
76 | return this;
77 | }
78 |
79 | @Override
80 | public LoginOAuth setAdditionalScopes(String scopes) {
81 | this.scopes += " " + scopes;
82 | return this;
83 | }
84 |
85 | @Override
86 | public LoginOAuth setRedirectUri(String redirectUri) {
87 | this.redirectUri = redirectUri;
88 | return this;
89 | }
90 |
91 | @Override
92 | public LoginOAuth setCallback(OnLoginCallback callback) {
93 | this.callback = callback;
94 | return this;
95 | }
96 |
97 | @Override
98 | public void init() {
99 | // Generating a complex state for better security
100 | // http://twobotechnologies.com/blog/2014/02/importance-of-state-in-oauth2.html
101 | final String state = UUID.randomUUID().toString();
102 |
103 | final OAuth20Service service = new ServiceBuilder()
104 | .apiKey(clientId)
105 | .apiSecret(clientSecret)
106 | .callback(redirectUri)
107 | .state(state)
108 | .scope(scopes)
109 | .build(oAuth.api);
110 | final String authUrl = service.getAuthorizationUrl();
111 |
112 | ConsentDialog.newInstance(authUrl, state)
113 | .setOnGetCodeCallback(new OnGetCodeCallback() {
114 | @Override
115 | public void onSuccess(final String code) {
116 | AsyncTask.execute(new Runnable() {
117 | @Override
118 | public void run() {
119 | try {
120 | final OAuth2AccessToken accessToken = service
121 | .getAccessToken(code);
122 | final SocialUser account = oAuth.getAccount(service,
123 | accessToken);
124 | oAuth.activity.runOnUiThread(new Runnable() {
125 | @Override
126 | public void run() {
127 | callback.onSuccess(accessToken.getAccessToken(), account);
128 | }
129 | });
130 | } catch (final Exception e) {
131 | oAuth.activity.runOnUiThread(new Runnable() {
132 | @Override
133 | public void run() {
134 | callback.onError(e);
135 | }
136 | });
137 | }
138 | }
139 | });
140 | }
141 |
142 | @Override
143 | public void onError(Exception error) {
144 | callback.onError(error);
145 | }
146 | })
147 | .show(oAuth.activity.getFragmentManager(), ConsentDialog.class.getName());
148 | }
149 |
150 | }
151 |
152 | public static class LogoutOAuth implements ILogoutOAuth {
153 | protected BaseOAuth oAuth;
154 |
155 | protected String token;
156 | protected OnLogoutCallback callback;
157 |
158 | protected LogoutOAuth(BaseOAuth oAuth) {
159 | this.oAuth = oAuth;
160 | }
161 |
162 | @Override
163 | public LogoutOAuth setToken(String token) {
164 | this.token = token;
165 | return this;
166 | }
167 |
168 | @Override
169 | public LogoutOAuth setCallback(OnLogoutCallback callback) {
170 | this.callback = callback;
171 | return this;
172 | }
173 |
174 | @Override
175 | public void init() {
176 | final String url = String.format(oAuth.revokeTokenUrl, token);
177 | final SuccessCallback requestCallback = new SuccessCallback() {
178 | @Override
179 | protected void onDone(final boolean success) {
180 | oAuth.activity.runOnUiThread(new Runnable() {
181 | @Override
182 | public void run() {
183 | if (success) {
184 | callback.onSuccess();
185 | } else {
186 | callback.onError(new Exception(getErrorMessage()));
187 | }
188 | }
189 | });
190 | }
191 | };
192 | AsyncTask.execute(new Runnable() {
193 | @Override
194 | public void run() {
195 | switch (oAuth.revokeTokenVerb) {
196 | case GET:
197 | HttpAgent.get(url)
198 | .go(requestCallback);
199 | break;
200 | case DELETE:
201 | HttpAgent.delete(url)
202 | .go(requestCallback);
203 | break;
204 | default:
205 | oAuth.activity.runOnUiThread(new Runnable() {
206 | @Override
207 | public void run() {
208 | callback.onError(new Exception("HTTP Verb not implemented: " + oAuth.revokeTokenVerb));
209 | }
210 | });
211 | }
212 | }
213 | });
214 | }
215 |
216 | }
217 |
218 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/FacebookOAuth.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | import android.app.Activity;
4 |
5 | import com.github.scribejava.apis.FacebookApi;
6 | import com.github.scribejava.core.model.Verb;
7 |
8 | import org.json.JSONObject;
9 |
10 | import java.text.SimpleDateFormat;
11 |
12 | import cafe.adriel.androidoauth.model.SocialUser;
13 |
14 | public final class FacebookOAuth extends BaseOAuth {
15 | private static final String DEFAULT_SCOPES = "public_profile email";
16 | private static final String GET_ACCOUNT_URL = "https://graph.facebook.com/v2.6/me?fields=name,email,picture,cover";
17 | private static final String REVOKE_TOKEN_URL = "https://graph.facebook.com/v2.6/me/permissions?access_token=%s";
18 | private static final Verb REVOKE_TOKEN_VERB = Verb.DELETE;
19 |
20 | private FacebookOAuth(Activity activity) {
21 | super(activity, FacebookApi.instance(), GET_ACCOUNT_URL, REVOKE_TOKEN_URL,
22 | REVOKE_TOKEN_VERB);
23 | }
24 |
25 | public static LoginOAuth login(Activity activity) {
26 | return new LoginOAuth(new FacebookOAuth(activity), DEFAULT_SCOPES);
27 | }
28 |
29 | public static LogoutOAuth logout(Activity activity) {
30 | return new LogoutOAuth(new FacebookOAuth(activity));
31 | }
32 |
33 | @Override
34 | protected SocialUser toAccount(String json) {
35 | try {
36 | JSONObject accountJson = new JSONObject(json);
37 | SocialUser account = new SocialUser();
38 | account.setId(accountJson.getString("id"));
39 | account.setName(accountJson.getString("name"));
40 | if (accountJson.has("email")) {
41 | account.setEmail(accountJson.getString("email"));
42 | }
43 | if (accountJson.has("picture")) {
44 | account.setPictureUrl(accountJson
45 | .getJSONObject("picture")
46 | .getJSONObject("data")
47 | .getString("url"));
48 | }
49 | if (accountJson.has("cover")) {
50 | account.setCoverUrl(accountJson
51 | .getJSONObject("cover")
52 | .getString("source"));
53 | }
54 | if(accountJson.has("birthday")){
55 | try {
56 | account.setBirthday(new SimpleDateFormat("MM/dd/yyyy")
57 | .parse(accountJson.getString("birthday")));
58 | } catch (Exception e){ }
59 | }
60 | account.setProvider(OAuthProvider.FACEBOOK);
61 | return account;
62 | } catch (Exception e) {
63 | e.printStackTrace();
64 | return null;
65 | }
66 | }
67 |
68 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/GoogleOAuth.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | import android.app.Activity;
4 | import android.util.Log;
5 |
6 | import com.github.scribejava.apis.GoogleApi20;
7 | import com.github.scribejava.core.model.Verb;
8 |
9 | import org.json.JSONObject;
10 |
11 | import java.text.SimpleDateFormat;
12 |
13 | import cafe.adriel.androidoauth.model.SocialUser;
14 |
15 | public final class GoogleOAuth extends BaseOAuth {
16 | private static final String DEFAULT_SCOPES =
17 | "https://www.googleapis.com/auth/userinfo.profile " +
18 | "https://www.googleapis.com/auth/userinfo.email";
19 | private static final String GET_ACCOUNT_URL = "https://www.googleapis.com/plus/v1/people/me";
20 | private static final String REVOKE_TOKEN_URL = "https://accounts.google.com/o/oauth2/revoke?token=%s";
21 | private static final Verb REVOKE_TOKEN_VERB = Verb.GET;
22 |
23 | private GoogleOAuth(Activity activity) {
24 | super(activity, GoogleApi20.instance(), GET_ACCOUNT_URL, REVOKE_TOKEN_URL,
25 | REVOKE_TOKEN_VERB);
26 | }
27 |
28 | public static LoginOAuth login(Activity activity) {
29 | return new LoginOAuth(new GoogleOAuth(activity), DEFAULT_SCOPES);
30 | }
31 |
32 | public static LogoutOAuth logout(Activity activity) {
33 | return new LogoutOAuth(new GoogleOAuth(activity));
34 | }
35 |
36 | @Override
37 | protected SocialUser toAccount(String json) {
38 | try {
39 | Log.e("GOOGLE", json+"");
40 | JSONObject accountJson = new JSONObject(json);
41 | SocialUser account = new SocialUser();
42 | account.setId(accountJson.getString("id"));
43 | account.setName(accountJson.getString("displayName"));
44 | if (accountJson.has("emails")) {
45 | account.setEmail(accountJson
46 | .getJSONArray("emails")
47 | .getJSONObject(0)
48 | .getString("value"));
49 | }
50 | if (accountJson.has("image")) {
51 | account.setPictureUrl(accountJson
52 | .getJSONObject("image")
53 | .getString("url"));
54 | }
55 | if (accountJson.has("cover")) {
56 | account.setCoverUrl(accountJson
57 | .getJSONObject("cover")
58 | .getJSONObject("coverPhoto")
59 | .getString("url"));
60 | }
61 | if(accountJson.has("birthday")){
62 | try {
63 | account.setBirthday(new SimpleDateFormat("yyyy-MM-dd")
64 | .parse(accountJson.getString("birthday")));
65 | } catch (Exception e){ }
66 | }
67 | account.setProvider(OAuthProvider.GOOGLE);
68 | return account;
69 | } catch (Exception e) {
70 | e.printStackTrace();
71 | return null;
72 | }
73 | }
74 |
75 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/ILoginOAuth.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | import cafe.adriel.androidoauth.callback.OnLoginCallback;
4 |
5 | public interface ILoginOAuth {
6 |
7 | T setClientId(String clientId);
8 |
9 | T setClientSecret(String clientSecret);
10 |
11 | T setAdditionalScopes(String scopes);
12 |
13 | T setRedirectUri(String redirectUri);
14 |
15 | T setCallback(OnLoginCallback callback);
16 |
17 | void init();
18 |
19 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/ILogoutOAuth.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | import cafe.adriel.androidoauth.callback.OnLogoutCallback;
4 |
5 | public interface ILogoutOAuth {
6 |
7 | T setToken(String token);
8 |
9 | T setCallback(OnLogoutCallback callback);
10 |
11 | void init();
12 |
13 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/oauth/OAuthProvider.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.oauth;
2 |
3 | public enum OAuthProvider {
4 | GOOGLE,
5 | FACEBOOK
6 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/view/ConsentDialog.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.view;
2 |
3 | import android.app.AlertDialog;
4 | import android.app.Dialog;
5 | import android.app.DialogFragment;
6 | import android.content.DialogInterface;
7 | import android.net.Uri;
8 | import android.os.Bundle;
9 | import android.webkit.WebChromeClient;
10 | import android.webkit.WebView;
11 | import android.webkit.WebViewClient;
12 |
13 | import cafe.adriel.androidoauth.callback.OnGetCodeCallback;
14 |
15 | public class ConsentDialog extends DialogFragment {
16 | private static final String EXTRA_AUTH_URL = "authUrl";
17 | private static final String EXTRA_STATE = "originalState";
18 |
19 | private String authUrl;
20 | private String originalState;
21 | private String code;
22 | private OnGetCodeCallback callback;
23 |
24 | public static ConsentDialog newInstance(String authUrl, String state) {
25 | Bundle args = new Bundle();
26 | args.putString(EXTRA_AUTH_URL, authUrl);
27 | args.putString(EXTRA_STATE, state);
28 | ConsentDialog frag = new ConsentDialog();
29 | frag.setArguments(args);
30 | return frag;
31 | }
32 |
33 | @Override
34 | public void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | authUrl = getArguments().getString(EXTRA_AUTH_URL);
37 | originalState = getArguments().getString(EXTRA_STATE);
38 | }
39 |
40 | @Override
41 | public Dialog onCreateDialog(Bundle savedInstanceState) {
42 | KeyboardWebView webView = new KeyboardWebView(getActivity());
43 | webView.setFocusable(true);
44 | webView.setFocusableInTouchMode(true);
45 | webView.getSettings().setJavaScriptEnabled(true);
46 | webView.getSettings().setUseWideViewPort(true);
47 | webView.getSettings().setSupportZoom(false);
48 | webView.setWebChromeClient(new WebChromeClient());
49 | webView.setWebViewClient(new WebViewClient() {
50 | @Override
51 | public void onPageFinished(WebView view, String url) {
52 | super.onPageFinished(view, url);
53 | if(view != null && url != null) {
54 | if (url.contains("code=")) {
55 | getCode(url);
56 | } else if (view.getTitle().contains("code=")) {
57 | // Creating a valid URI to extract parameters easily
58 | getCode(view.getTitle().replace("Success ", "http://oauth?"));
59 | }
60 | }
61 | }
62 | });
63 | webView.loadUrl(authUrl);
64 |
65 | return new AlertDialog.Builder(getActivity())
66 | .setView(webView)
67 | .create();
68 | }
69 |
70 | @Override
71 | public void onDismiss(DialogInterface dialog) {
72 | super.onDismiss(dialog);
73 | if (callback != null && (code == null || code.isEmpty())) {
74 | callback.onError(new Exception(
75 | "Dialog was dismissed without complete the authentication"));
76 | }
77 | }
78 |
79 | public ConsentDialog setOnGetCodeCallback(OnGetCodeCallback callback) {
80 | this.callback = callback;
81 | return this;
82 | }
83 |
84 | private void getCode(String url) {
85 | Uri uri = Uri.parse(url);
86 | String receivedState = uri.getQueryParameter("state");
87 | code = uri.getQueryParameter("code");
88 | if (callback != null) {
89 | if (code != null && !code.isEmpty() && originalState.equals(receivedState)) {
90 | callback.onSuccess(code);
91 | } else {
92 | String error = String.format("Wrong state: %s (original) != %s (received)",
93 | originalState, receivedState);
94 | callback.onError(new Exception(error));
95 | }
96 | }
97 | dismiss();
98 | }
99 |
100 | }
--------------------------------------------------------------------------------
/lib/src/main/java/cafe/adriel/androidoauth/view/KeyboardWebView.java:
--------------------------------------------------------------------------------
1 | package cafe.adriel.androidoauth.view;
2 |
3 | import android.content.Context;
4 | import android.view.MotionEvent;
5 | import android.webkit.WebView;
6 |
7 | /*
8 | By RuslanK
9 | http://stackoverflow.com/a/13418767
10 | */
11 | public class KeyboardWebView extends WebView {
12 | public KeyboardWebView(Context context) {
13 | super(context);
14 | }
15 |
16 | @Override
17 | public boolean onCheckIsTextEditor() {
18 | return true;
19 | }
20 |
21 | @Override
22 | public boolean onTouchEvent(MotionEvent ev) {
23 | switch (ev.getAction()) {
24 | case MotionEvent.ACTION_DOWN:
25 | case MotionEvent.ACTION_UP:
26 | if (!hasFocus()) {
27 | requestFocus();
28 | }
29 | break;
30 | }
31 | return super.onTouchEvent(ev);
32 | }
33 | }
--------------------------------------------------------------------------------
/lib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidOAuth
3 |
4 |
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/logo.png
--------------------------------------------------------------------------------
/screenshots/facebook-auth.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/screenshots/facebook-auth.jpg
--------------------------------------------------------------------------------
/screenshots/facebook-consent.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/screenshots/facebook-consent.jpg
--------------------------------------------------------------------------------
/screenshots/google-auth.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/screenshots/google-auth.jpg
--------------------------------------------------------------------------------
/screenshots/google-consent.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/bdc75fcd3630ae97008b67a1a910bd7f3694aebc/screenshots/google-consent.jpg
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lib'
2 |
--------------------------------------------------------------------------------