├── .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 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-AndroidOAuth-green.svg?style=true)](https://android-arsenal.com/details/1/3837) [![Release](https://jitpack.io/v/adrielcafe/AndroidOAuth.svg)](https://jitpack.io/#adrielcafe/AndroidOAuth) 2 | 3 | ![AndroidOAuth](https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/master/logo.png) 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 | ![Google Consent](https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/master/screenshots/google-consent.jpg) ![Google Auth](https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/master/screenshots/google-auth.jpg) 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 | ![Facebook Consent](https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/master/screenshots/facebook-consent.jpg) ![Facebook Auth](https://raw.githubusercontent.com/adrielcafe/AndroidOAuth/master/screenshots/facebook-auth.jpg) 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 |