Commit e028058e authored by Paul Bird's avatar Paul Bird

Added refresh access token

parent 5555db35
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -23,7 +23,6 @@ import okhttp3.Response; ...@@ -23,7 +23,6 @@ import okhttp3.Response;
public class Authenticate { public class Authenticate {
private String oauth2URL = "https://api.occupie.com/oauth2/token"; private String oauth2URL = "https://api.occupie.com/oauth2/token";
private String clientSecret = "";
private SharedPrefs sharedPrefs; private SharedPrefs sharedPrefs;
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
...@@ -33,10 +32,10 @@ public class Authenticate { ...@@ -33,10 +32,10 @@ public class Authenticate {
sharedPrefs = new SharedPrefs(context); sharedPrefs = new SharedPrefs(context);
} }
public void login(String username, String password) { public void login(String clientId, String clientSecret, String username, String password) {
String postBody = "grant_type=password" String postBody = "grant_type=password"
+ "&client_id=trustedclient" + "&client_id=" + clientId
+ "&client_secret=" + clientSecret + "&client_secret=" + clientSecret
+ "&scope=hubapi" + "&scope=hubapi"
+ "&username=" + username + "&password=" + password; + "&username=" + username + "&password=" + password;
...@@ -48,8 +47,23 @@ public class Authenticate { ...@@ -48,8 +47,23 @@ public class Authenticate {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
public void refreshAccessToken(String clientId, String clientSecret, String refreshToken) {
String postBody = "grant_type=refresh_token"
+ "&client_id=" + clientId
+ "&client_secret=" + clientSecret
+ "&scope=hubapi"
+ "&refresh_token=" + refreshToken;
Log.d("postBody", postBody);
try {
postRequest(oauth2URL, postBody);
} catch (IOException e) {
e.printStackTrace();
}
} }
...@@ -89,6 +103,44 @@ public class Authenticate { ...@@ -89,6 +103,44 @@ public class Authenticate {
}); });
} }
/*
void refreshAccessTokenPostRequest(String postUrl, String postBody) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(FORM_ENCODED, postBody);
Request request = new Request.Builder()
.url(postUrl)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String theResponse = response.body().string();
Log.d("theResponse",theResponse);
try {
JSONObject json = new JSONObject(theResponse);
final String accesstoken = json.getString("access_token");
final String refreshtoken = json.getString("refresh_token");
saveTheTokens(accesstoken, refreshtoken);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
*/
void saveTheTokens(String accessToken, String refreshToken) { void saveTheTokens(String accessToken, String refreshToken) {
sharedPrefs.setAccessToken(accessToken); sharedPrefs.setAccessToken(accessToken);
sharedPrefs.setRefreshToken(refreshToken); sharedPrefs.setRefreshToken(refreshToken);
......
...@@ -14,6 +14,15 @@ import android.widget.Button; ...@@ -14,6 +14,15 @@ import android.widget.Button;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
//
// Need to provide credentials below
//
private String clientId = "";
private String clientSecret = "";
private String userId = "";
private String userPassword = "";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -30,13 +39,20 @@ public class MainActivity extends AppCompatActivity { ...@@ -30,13 +39,20 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
final Button button = (Button) findViewById(R.id.loginButton); final Button loginButton = (Button) findViewById(R.id.loginButton);
button.setOnClickListener(new View.OnClickListener() { loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
login(); login();
} }
}); });
final Button refreshAccessTokenButton = (Button) findViewById(R.id.refreshAccessTokenButton);
refreshAccessTokenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
refreshAccessToken();
}
});
final Button getArea1 = (Button) findViewById(R.id.getArea1); final Button getArea1 = (Button) findViewById(R.id.getArea1);
getArea1.setOnClickListener(new View.OnClickListener() { getArea1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
...@@ -87,7 +103,14 @@ public class MainActivity extends AppCompatActivity { ...@@ -87,7 +103,14 @@ public class MainActivity extends AppCompatActivity {
private void login() { private void login() {
Authenticate authenticate = new Authenticate(this); Authenticate authenticate = new Authenticate(this);
authenticate.login("user@domain.com", "yourpassword"); authenticate.login(clientId, clientSecret, userId, userPassword);
}
private void refreshAccessToken() {
SharedPrefs sharedPrefs = new SharedPrefs(this);
String refreshToken = sharedPrefs.getRefreshToken();
Authenticate authenticate = new Authenticate(this);
authenticate.refreshAccessToken(clientId,clientSecret, refreshToken);
} }
private void getArea() { private void getArea() {
......
...@@ -34,4 +34,7 @@ public class SharedPrefs extends Activity { ...@@ -34,4 +34,7 @@ public class SharedPrefs extends Activity {
public String getAccessToken() { public String getAccessToken() {
return preferences.getString("accessToken",""); return preferences.getString("accessToken","");
} }
public String getRefreshToken() {
return preferences.getString("refreshToken","");
}
} }
...@@ -14,33 +14,43 @@ ...@@ -14,33 +14,43 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Get Access Token" android:text="Get Access Token"
app:layout_constraintBottom_toTopOf="parent" android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/> app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/refreshAccessTokenButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh Access Token"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/loginButton" />
<Button <Button
android:id="@+id/getArea1" android:id="@+id/getArea1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="GET area/1" android:text="GET area/1"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/loginButton" app:layout_constraintTop_toBottomOf="@+id/refreshAccessTokenButton" />
app:layout_constraintVertical_bias="0.062" />
<Button <Button
android:id="@+id/putAccount1" android:id="@+id/putAccount1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="PUT account" android:text="PUT account"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="10dp"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/getArea1" app:layout_constraintTop_toBottomOf="@id/getArea1" />
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintVertical_bias="0.114" />
<Button <Button
...@@ -48,11 +58,9 @@ ...@@ -48,11 +58,9 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="DELETE account" android:text="DELETE account"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="10dp"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/putAccount1" app:layout_constraintTop_toBottomOf="@id/putAccount1" />
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintVertical_bias="0.125" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment