Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
operate-android-java
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
examples
operate-android-java
Commits
e028058e
Commit
e028058e
authored
Aug 31, 2017
by
Paul Bird
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added refresh access token
parent
5555db35
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
111 additions
and
18 deletions
+111
-18
misc.xml
.idea/misc.xml
+1
-1
vcs.xml
.idea/vcs.xml
+7
-0
Authenticate.java
...in/java/tech/essensys/operateapiexample/Authenticate.java
+55
-3
MainActivity.java
...in/java/tech/essensys/operateapiexample/MainActivity.java
+26
-3
SharedPrefs.java
...ain/java/tech/essensys/operateapiexample/SharedPrefs.java
+3
-0
content_main.xml
app/src/main/res/layout/content_main.xml
+19
-11
No files found.
.idea/misc.xml
View file @
e028058e
...
...
@@ -37,7 +37,7 @@
<ConfirmationsSetting
value=
"0"
id=
"Add"
/>
<ConfirmationsSetting
value=
"0"
id=
"Remove"
/>
</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"
/>
</component>
<component
name=
"ProjectType"
>
...
...
.idea/vcs.xml
0 → 100644
View file @
e028058e
<?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
app/src/main/java/tech/essensys/operateapiexample/Authenticate.java
View file @
e028058e
...
...
@@ -23,7 +23,6 @@ import okhttp3.Response;
public
class
Authenticate
{
private
String
oauth2URL
=
"https://api.occupie.com/oauth2/token"
;
private
String
clientSecret
=
""
;
private
SharedPrefs
sharedPrefs
;
public
static
final
MediaType
JSON
=
MediaType
.
parse
(
"application/json; charset=utf-8"
);
...
...
@@ -33,10 +32,10 @@ public class Authenticate {
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"
+
"&client_id=
trustedclient"
+
"&client_id=
"
+
clientId
+
"&client_secret="
+
clientSecret
+
"&scope=hubapi"
+
"&username="
+
username
+
"&password="
+
password
;
...
...
@@ -48,8 +47,23 @@ public class Authenticate {
}
catch
(
IOException
e
)
{
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 {
});
}
/*
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
)
{
sharedPrefs
.
setAccessToken
(
accessToken
);
sharedPrefs
.
setRefreshToken
(
refreshToken
);
...
...
app/src/main/java/tech/essensys/operateapiexample/MainActivity.java
View file @
e028058e
...
...
@@ -14,6 +14,15 @@ import android.widget.Button;
public
class
MainActivity
extends
AppCompatActivity
{
//
// Need to provide credentials below
//
private
String
clientId
=
""
;
private
String
clientSecret
=
""
;
private
String
userId
=
""
;
private
String
userPassword
=
""
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
...
...
@@ -30,13 +39,20 @@ public class MainActivity extends AppCompatActivity {
}
});
final
Button
b
utton
=
(
Button
)
findViewById
(
R
.
id
.
loginButton
);
b
utton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
final
Button
loginB
utton
=
(
Button
)
findViewById
(
R
.
id
.
loginButton
);
loginB
utton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
public
void
onClick
(
View
v
)
{
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
);
getArea1
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
public
void
onClick
(
View
v
)
{
...
...
@@ -87,7 +103,14 @@ public class MainActivity extends AppCompatActivity {
private
void
login
()
{
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
()
{
...
...
app/src/main/java/tech/essensys/operateapiexample/SharedPrefs.java
View file @
e028058e
...
...
@@ -34,4 +34,7 @@ public class SharedPrefs extends Activity {
public
String
getAccessToken
()
{
return
preferences
.
getString
(
"accessToken"
,
""
);
}
public
String
getRefreshToken
()
{
return
preferences
.
getString
(
"refreshToken"
,
""
);
}
}
app/src/main/res/layout/content_main.xml
View file @
e028058e
...
...
@@ -14,33 +14,43 @@
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
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_constraintRight_toRightOf=
"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
android:id=
"@+id/getArea1"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"GET area/1"
android:layout_marginBottom=
"10dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@id/loginButton"
app:layout_constraintVertical_bias=
"0.062"
/>
app:layout_constraintTop_toBottomOf=
"@+id/refreshAccessTokenButton"
/>
<Button
android:id=
"@+id/putAccount1"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"PUT account"
a
pp:layout_constraintBottom_toBottomOf=
"parent
"
a
ndroid:layout_marginBottom=
"10dp
"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@id/getArea1"
app:layout_constraintHorizontal_bias=
"0.501"
app:layout_constraintVertical_bias=
"0.114"
/>
app:layout_constraintTop_toBottomOf=
"@id/getArea1"
/>
<Button
...
...
@@ -48,11 +58,9 @@
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"DELETE account"
a
pp:layout_constraintBottom_toBottomOf=
"parent
"
a
ndroid:layout_marginBottom=
"10dp
"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@id/putAccount1"
app:layout_constraintHorizontal_bias=
"0.502"
app:layout_constraintVertical_bias=
"0.125"
/>
app:layout_constraintTop_toBottomOf=
"@id/putAccount1"
/>
</android.support.constraint.ConstraintLayout>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment