├── .gitignore ├── LICENSE ├── README.md ├── Retrofit.OAuth-2.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── retrofitwrapper │ │ └── retrofitoauth_2 │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ ├── oauth2 │ │ ├── client │ │ │ ├── ApiService.java │ │ │ ├── OauthService.java │ │ │ ├── RequestInterceptorService.java │ │ │ ├── SignUpService.java │ │ │ └── UserService.java │ │ ├── constant │ │ │ └── OauthConstant.java │ │ ├── request │ │ │ ├── AccessTokenRequest.java │ │ │ └── SignUpRequest.java │ │ ├── response │ │ │ ├── AccessTokenResponse.java │ │ │ ├── ApiResponse.java │ │ │ ├── BaseResponse.java │ │ │ ├── RefreshTokenResponse.java │ │ │ ├── SignUpResponse.java │ │ │ └── UserResponse.java │ │ └── service │ │ │ ├── IApiService.java │ │ │ ├── IOauthService.java │ │ │ ├── ISignUpService.java │ │ │ └── IUserService.java │ └── ui │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | #Idea files 30 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 gökhan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Retrofit-OAuth-2-Resource-Owner-Password-Credentials 2 | Simple OAuth2 Resource Owner Password Credentials. 3 | 4 | 5 | 6 | #GetAccessToken 7 | 8 | This will add the necessary oAuth headers to each request. 9 | ```java 10 | public interface IOauthService { 11 | 12 | @POST("/oauth/token") 13 | void getAccessToken(@Body AccessTokenRequest accessTokenRequest, 14 | Callback responseCallback); 15 | } 16 | ``` 17 | 18 | #Send Token Every Request 19 | 20 | ```java 21 | RequestInterceptor requestInterceptor = new RequestInterceptor() { 22 | @Override 23 | public void intercept(RequestFacade request) { 24 | request.addHeader("Authorization", "Bearer" + token); 25 | request.addHeader("User-Agent", "Android"); 26 | } 27 | }; 28 | ``` 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Retrofit.OAuth-2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion "21.1.2" 6 | 7 | defaultConfig { 8 | applicationId "retrofitwrapper.retrofitoauth_2" 9 | minSdkVersion 14 10 | targetSdkVersion 21 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 fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:22.0.0' 25 | compile 'com.squareup.retrofit:retrofit:1.9.0' 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /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/gokhan/Library/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/androidTest/java/retrofitwrapper/retrofitoauth_2/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package retrofitwrapper.retrofitoauth_2; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/client/ApiService.java: -------------------------------------------------------------------------------- 1 | package oauth2.client; 2 | 3 | import oauth2.constant.OauthConstant; 4 | import oauth2.service.IApiService; 5 | import retrofit.RestAdapter; 6 | 7 | /** 8 | * Created by gokhan on 4/18/15. 9 | */ 10 | public class ApiService { 11 | 12 | private IApiService _apiService; 13 | 14 | 15 | public IApiService getMessage() { 16 | RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) 17 | .setEndpoint(OauthConstant.AUTHENTICATION_SERVER_URL). 18 | setRequestInterceptor(new RequestInterceptorService().requestInterceptor) 19 | .build(); 20 | _apiService = restAdapter.create(IApiService.class); 21 | 22 | 23 | return _apiService; 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/client/OauthService.java: -------------------------------------------------------------------------------- 1 | package oauth2.client; 2 | 3 | import oauth2.constant.OauthConstant; 4 | import oauth2.service.IOauthService; 5 | import retrofit.RestAdapter; 6 | 7 | /** 8 | * Created by gokhan on 4/8/15. 9 | */ 10 | public class OauthService { 11 | 12 | private IOauthService _oauthService; 13 | 14 | 15 | public IOauthService getAccessToken() { 16 | RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) 17 | .setEndpoint(OauthConstant.AUTHENTICATION_SERVER_URL) 18 | .build(); 19 | _oauthService = restAdapter.create(IOauthService.class); 20 | 21 | 22 | return _oauthService; 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/java/oauth2/client/RequestInterceptorService.java: -------------------------------------------------------------------------------- 1 | package oauth2.client; 2 | 3 | import retrofit.RequestInterceptor; 4 | import ui.MainActivity; 5 | 6 | /** 7 | * Created by gokhan on 4/15/15. 8 | */ 9 | public class RequestInterceptorService{ 10 | 11 | RequestInterceptor requestInterceptor = new RequestInterceptor() { 12 | @Override 13 | public void intercept(RequestFacade request) { 14 | 15 | 16 | request.addHeader("Authorization", "Bearer"); 17 | request.addHeader("User-Agent", "Android"); 18 | } 19 | 20 | 21 | }; 22 | 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/client/SignUpService.java: -------------------------------------------------------------------------------- 1 | package oauth2.client; 2 | 3 | import oauth2.constant.OauthConstant; 4 | import oauth2.service.ISignUpService; 5 | import retrofit.RestAdapter; 6 | 7 | /** 8 | * Created by gokhan on 4/18/15. 9 | */ 10 | public class SignUpService { 11 | 12 | private ISignUpService _signUpService; 13 | 14 | 15 | public ISignUpService signUpService() { 16 | RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) 17 | .setEndpoint(OauthConstant.AUTHENTICATION_SERVER_URL). 18 | setRequestInterceptor(new RequestInterceptorService().requestInterceptor) 19 | .build(); 20 | _signUpService = restAdapter.create(ISignUpService.class); 21 | 22 | 23 | return _signUpService; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/client/UserService.java: -------------------------------------------------------------------------------- 1 | package oauth2.client; 2 | 3 | import oauth2.constant.OauthConstant; 4 | import oauth2.service.IUserService; 5 | import retrofit.RestAdapter; 6 | 7 | /** 8 | * Created by gokhan on 4/18/15. 9 | */ 10 | public class UserService { 11 | 12 | private IUserService _userService; 13 | 14 | 15 | public IUserService getUser() { 16 | RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) 17 | .setEndpoint(OauthConstant.AUTHENTICATION_SERVER_URL). 18 | setRequestInterceptor(new RequestInterceptorService().requestInterceptor) 19 | .build(); 20 | _userService = restAdapter.create(IUserService.class); 21 | 22 | 23 | return _userService; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/constant/OauthConstant.java: -------------------------------------------------------------------------------- 1 | package oauth2.constant; 2 | 3 | /** 4 | * Created by gokhan on 4/8/15. 5 | */ 6 | public class OauthConstant { 7 | 8 | public static final String ACCESS_TOKEN = "access_token"; 9 | public static final String CLIENT_ID = "client_id"; 10 | public static final String CLIENT_SECRET = "client_secret"; 11 | public static final String REFRESH_TOKEN = "refresh_token"; 12 | public static final String USERNAME = "username"; 13 | public static final String PASSWORD = "password"; 14 | public static final String AUTHENTICATION_SERVER_URL = "https://staj-io-goldenilkay92-1.c9.io/api/v1"; 15 | public static final String RESOURCE_SERVER_URL = "resource_server_url"; 16 | public static final String GRANT_TYPE = "grant_type"; 17 | public static final String SCOPE = "scope"; 18 | public static final String AUTHORIZATION = "Authorization"; 19 | public static final String BEARER = "Bearer"; 20 | public static final String BASIC = "Basic"; 21 | public static final String JSON_CONTENT = "application/json"; 22 | public static final String XML_CONTENT = "application/xml"; 23 | public static final String URL_ENCODED_CONTENT = "application/x-www-form-urlencoded"; 24 | public static final String EXPIRES_IN = "expires_in"; 25 | public static final String TOKEN_TYPE = "token_type"; 26 | public static final int HTTP_OK = 200; 27 | public static final int HTTP_FORBIDDEN = 403; 28 | public static final int HTTP_UNAUTHORIZED = 401; 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/request/AccessTokenRequest.java: -------------------------------------------------------------------------------- 1 | package oauth2.request; 2 | 3 | /** 4 | * Created by gokhan on 4/8/15. 5 | */ 6 | public class AccessTokenRequest { 7 | 8 | private String username; 9 | private String password; 10 | private String client_id; 11 | private String client_secret; 12 | private String grant_type; 13 | 14 | public void setUsername(String username) { 15 | this.username = username; 16 | } 17 | 18 | public void setPassword(String password) { 19 | this.password = password; 20 | } 21 | 22 | public void setClient_id(String client_id) { 23 | this.client_id = client_id; 24 | } 25 | 26 | public void setClient_secret(String client_secret) { 27 | this.client_secret = client_secret; 28 | } 29 | 30 | public void setGrant_type(String grant_type) { 31 | this.grant_type = grant_type; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/request/SignUpRequest.java: -------------------------------------------------------------------------------- 1 | package oauth2.request; 2 | 3 | /** 4 | * Created by gokhan on 4/18/15. 5 | */ 6 | public class SignUpRequest { 7 | private String name; 8 | private String email; 9 | private String username; 10 | private String password; 11 | 12 | public String getUsername() { 13 | return username; 14 | } 15 | 16 | public void setUsername(String username) { 17 | this.username = username; 18 | } 19 | 20 | public String getEmail() { 21 | return email; 22 | } 23 | 24 | public void setEmail(String email) { 25 | this.email = email; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public String getPassword() { 37 | return password; 38 | } 39 | 40 | public void setPassword(String password) { 41 | this.password = password; 42 | } 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/AccessTokenResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/8/15. 5 | */ 6 | public class AccessTokenResponse extends BaseResponse { 7 | 8 | private String access_token; 9 | private String refresh_token; 10 | private int expires_in; 11 | private String token_type; 12 | 13 | public String getAccess_token() { 14 | return access_token; 15 | } 16 | 17 | public String getRefresh_token() { 18 | return refresh_token; 19 | } 20 | 21 | public int getExpires_in() { 22 | return expires_in; 23 | } 24 | 25 | public String getToken_type() { 26 | return token_type; 27 | } 28 | 29 | 30 | @Override 31 | public String toString() { 32 | 33 | if (super.getError() != null && super.getError_description() != null) { 34 | return super.getError() + super.getError_description(); 35 | } 36 | return "AccessToken{" + 37 | "accessToken='" + access_token + '\'' + 38 | ", tokenType='" + token_type + '\'' + 39 | ", expiresIn=" + expires_in + 40 | ", refreshToken='" + refresh_token + '\'' + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/ApiResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/18/15. 5 | */ 6 | public class ApiResponse { 7 | private String msg; 8 | 9 | public String getMsg() { 10 | return msg; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/BaseResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/8/15. 5 | */ 6 | public abstract class BaseResponse { 7 | 8 | private String error = null; 9 | private String error_description = null; 10 | 11 | public String getError() { 12 | return error; 13 | } 14 | 15 | public String getError_description() { 16 | 17 | return error_description; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/RefreshTokenResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/8/15. 5 | */ 6 | public class RefreshTokenResponse extends BaseResponse { 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/SignUpResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/18/15. 5 | */ 6 | public class SignUpResponse { 7 | private String type; 8 | 9 | public String getData() { 10 | return data; 11 | } 12 | 13 | public String getType() { 14 | return type; 15 | } 16 | 17 | private String data; 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/response/UserResponse.java: -------------------------------------------------------------------------------- 1 | package oauth2.response; 2 | 3 | /** 4 | * Created by gokhan on 4/18/15. 5 | */ 6 | public class UserResponse { 7 | private String userId; 8 | private String username; 9 | private String name; 10 | 11 | public String getEmail() { 12 | return email; 13 | } 14 | 15 | public void setEmail(String email) { 16 | this.email = email; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public String getUserId() { 28 | return userId; 29 | } 30 | 31 | public void setUserId(String userId) { 32 | this.userId = userId; 33 | } 34 | 35 | public String getUsername() { 36 | return username; 37 | } 38 | 39 | public void setUsername(String username) { 40 | this.username = username; 41 | } 42 | 43 | private String email; 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/service/IApiService.java: -------------------------------------------------------------------------------- 1 | package oauth2.service; 2 | 3 | import oauth2.response.ApiResponse; 4 | import retrofit.Callback; 5 | import retrofit.http.GET; 6 | 7 | /** 8 | * Created by gokhan on 4/18/15. 9 | */ 10 | public interface IApiService { 11 | 12 | @GET("/") 13 | void getMessage(Callback responseCallback); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/service/IOauthService.java: -------------------------------------------------------------------------------- 1 | package oauth2.service; 2 | 3 | import oauth2.request.AccessTokenRequest; 4 | import oauth2.response.AccessTokenResponse; 5 | import retrofit.Callback; 6 | import retrofit.http.Body; 7 | import retrofit.http.POST; 8 | 9 | 10 | public interface IOauthService { 11 | 12 | @POST("/oauth/token") 13 | void getAccessToken(@Body AccessTokenRequest accessTokenRequest, 14 | Callback responseCallback); 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/service/ISignUpService.java: -------------------------------------------------------------------------------- 1 | package oauth2.service; 2 | 3 | import oauth2.request.SignUpRequest; 4 | import oauth2.response.SignUpResponse; 5 | import retrofit.Callback; 6 | import retrofit.http.Body; 7 | import retrofit.http.POST; 8 | 9 | /** 10 | * Created by gokhan on 4/18/15. 11 | */ 12 | public interface ISignUpService { 13 | @POST("/signup") 14 | void signUp(@Body SignUpRequest signUpRequest, 15 | Callback signUpResponseCallback); 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/oauth2/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package oauth2.service; 2 | 3 | import oauth2.response.UserResponse; 4 | import retrofit.Callback; 5 | import retrofit.http.GET; 6 | 7 | /** 8 | * Created by gokhan on 4/18/15. 9 | */ 10 | public interface IUserService { 11 | 12 | @GET("/user") 13 | void user(Callback userResponseCallback); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.util.Log; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import oauth2.client.ApiService; 15 | import oauth2.client.OauthService; 16 | import oauth2.client.SignUpService; 17 | import oauth2.client.UserService; 18 | import oauth2.request.AccessTokenRequest; 19 | import oauth2.request.SignUpRequest; 20 | import oauth2.response.AccessTokenResponse; 21 | import oauth2.response.ApiResponse; 22 | import oauth2.response.SignUpResponse; 23 | import oauth2.response.UserResponse; 24 | import retrofit.Callback; 25 | import retrofit.RetrofitError; 26 | import retrofit.client.Response; 27 | import retrofitwrapper.retrofitoauth_2.R; 28 | 29 | 30 | public class MainActivity extends ActionBarActivity { 31 | private Button btnSignUp; 32 | private EditText txtUsername; 33 | private EditText txtEmail; 34 | private EditText txtPassword; 35 | private EditText txtName; 36 | private EditText txtUsernameSignIn; 37 | private EditText txtPasswordSignIn; 38 | private Button btnSignIn; 39 | private TextView txtUserDetail; 40 | private Button btnUserDetail; 41 | private static final String TAG = MainActivity.class.getSimpleName(); 42 | 43 | public MainActivity() { 44 | } 45 | 46 | @Override 47 | protected void onCreate(Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | 50 | setContentView(R.layout.activity_main); 51 | btnSignUp = (Button) findViewById(R.id.btnSignUp); 52 | txtUsername = (EditText) findViewById(R.id.txtUsername); 53 | txtEmail = (EditText) findViewById(R.id.txtEmail); 54 | txtPassword = (EditText) findViewById(R.id.txtPassword); 55 | txtName = (EditText) findViewById(R.id.txtName); 56 | txtUsernameSignIn = (EditText) findViewById(R.id.txtUsernameSignIn); 57 | txtPasswordSignIn = (EditText) findViewById(R.id.txtPasswordSignIn); 58 | txtUserDetail = (TextView) findViewById(R.id.txtUserDetail); 59 | btnSignIn = (Button) findViewById(R.id.btnSignIn); 60 | btnUserDetail = (Button) findViewById(R.id.btnUserDetail); 61 | 62 | btnSignUp.setOnClickListener(new View.OnClickListener() { 63 | @Override 64 | public void onClick(View v) { 65 | signUp(); 66 | } 67 | }); 68 | 69 | btnSignIn.setOnClickListener(new View.OnClickListener() { 70 | @Override 71 | public void onClick(View v) { 72 | getAccessToken(); 73 | } 74 | }); 75 | 76 | btnUserDetail.setOnClickListener(new View.OnClickListener() { 77 | @Override 78 | public void onClick(View v) { 79 | getUser(); 80 | } 81 | }); 82 | 83 | } 84 | 85 | 86 | public void getUser() { 87 | UserService userService = new UserService(); 88 | userService.getUser().user(new Callback() { 89 | @Override 90 | public void success(UserResponse userResponse, Response response) { 91 | txtUserDetail.setText("USERNAME:" + userResponse.getUsername() 92 | + "/n" + "NAME:" + userResponse.getUsername()); 93 | } 94 | 95 | @Override 96 | public void failure(RetrofitError error) { 97 | 98 | } 99 | }); 100 | } 101 | 102 | public void getAccessToken() { 103 | AccessTokenRequest accessTokenRequest = new AccessTokenRequest(); 104 | accessTokenRequest.setClient_id("client"); 105 | accessTokenRequest.setClient_secret("client"); 106 | accessTokenRequest.setGrant_type("password"); 107 | accessTokenRequest.setUsername(txtUsernameSignIn.getText().toString()); 108 | accessTokenRequest.setPassword(txtPasswordSignIn.getText().toString()); 109 | OauthService service = new OauthService(); 110 | 111 | service.getAccessToken().getAccessToken(accessTokenRequest, new Callback() { 112 | @Override 113 | public void success(AccessTokenResponse accessTokenResponse, Response response) { 114 | 115 | if (accessTokenResponse.getAccess_token() == null) { 116 | Toast.makeText(getApplicationContext(), accessTokenResponse.getError(), Toast.LENGTH_LONG).show(); 117 | 118 | } else { 119 | Toast.makeText(getApplicationContext(), 120 | accessTokenResponse.getAccess_token(), 121 | Toast.LENGTH_LONG).show(); 122 | } 123 | } 124 | 125 | @Override 126 | public void failure(RetrofitError error) { 127 | Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 128 | 129 | } 130 | }); 131 | } 132 | 133 | 134 | // 135 | // 136 | 137 | 138 | public void getMessage() { 139 | ApiService apiService = new ApiService(); 140 | apiService.getMessage().getMessage(new Callback() { 141 | @Override 142 | public void success(ApiResponse apiResponse, Response response) { 143 | Log.e(TAG, apiResponse.getMsg()); 144 | Toast.makeText(getApplicationContext(), apiResponse.getMsg(), Toast.LENGTH_LONG).show(); 145 | } 146 | 147 | @Override 148 | public void failure(RetrofitError error) { 149 | 150 | } 151 | }); 152 | } 153 | 154 | 155 | public void signUp() { 156 | 157 | SignUpRequest signUpRequest = new SignUpRequest(); 158 | signUpRequest.setEmail(txtEmail.getText().toString()); 159 | signUpRequest.setName(txtName.getText().toString()); 160 | signUpRequest.setPassword(txtPassword.getText().toString()); 161 | signUpRequest.setUsername(txtUsername.getText().toString()); 162 | SignUpService signUpService = new SignUpService(); 163 | signUpService.signUpService().signUp(signUpRequest, new Callback() { 164 | @Override 165 | public void success(SignUpResponse signUpResponse, Response response) { 166 | Toast.makeText(getApplicationContext(), signUpResponse.getData(), Toast.LENGTH_LONG).show(); 167 | } 168 | 169 | @Override 170 | public void failure(RetrofitError error) { 171 | 172 | } 173 | }); 174 | } 175 | 176 | @Override 177 | public boolean onCreateOptionsMenu(Menu menu) { 178 | getMenuInflater().inflate(R.menu.menu_main, menu); 179 | return true; 180 | } 181 | 182 | @Override 183 | public boolean onOptionsItemSelected(MenuItem item) { 184 | int id = item.getItemId(); 185 | 186 | if (id == R.id.action_settings) { 187 | return true; 188 | } 189 | 190 | return super.onOptionsItemSelected(item); 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 |