Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
operate-android-kotlin
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-kotlin
Commits
0e7b90cf
Commit
0e7b90cf
authored
Aug 31, 2017
by
Paul Bird
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add refresh access token
parent
afadd295
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
30 deletions
+86
-30
misc.xml
.idea/misc.xml
+1
-1
Authenticate.kt
...main/java/tech/essensys/operateapiexample/Authenticate.kt
+28
-6
MainActivity.kt
...main/java/tech/essensys/operateapiexample/MainActivity.kt
+20
-3
OperateAPI1.kt
.../main/java/tech/essensys/operateapiexample/OperateAPI1.kt
+6
-3
SharedPrefs.kt
.../main/java/tech/essensys/operateapiexample/SharedPrefs.kt
+12
-6
content_main.xml
app/src/main/res/layout/content_main.xml
+19
-11
No files found.
.idea/misc.xml
View file @
0e7b90cf
...
...
@@ -24,7 +24,7 @@
</value>
</option>
</component>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
8
"
default=
"true"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1_
7
"
default=
"true"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
</component>
<component
name=
"ProjectType"
>
...
...
app/src/main/java/tech/essensys/operateapiexample/Authenticate.kt
View file @
0e7b90cf
...
...
@@ -7,6 +7,7 @@ import org.json.JSONException
import
org.json.JSONObject
import
java.io.IOException
/**
* Created by paul on 22/08/2017.
*/
...
...
@@ -14,16 +15,17 @@ import java.io.IOException
class
Authenticate
(
context
:
Context
)
{
private
val
oauth2URL
=
"https://api.occupie.com/oauth2/token"
private
val
clientSecret
=
""
private
val
sharedPrefs
:
SharedPrefs
init
{
sharedPrefs
=
SharedPrefs
(
context
)
}
fun
login
(
username
:
String
,
password
:
String
)
{
val
postBody
=
"grant_type=password"
+
"&client_id=trustedclient"
+
"&client_secret="
+
clientSecret
+
"&scope=hubapi"
+
"&username="
+
username
+
"&password="
+
password
fun
login
(
clientId
:
String
,
clientSecret
:
String
,
username
:
String
,
password
:
String
)
{
val
postBody
=
"grant_type=password"
+
"&client_id="
+
clientId
+
"&client_secret="
+
clientSecret
+
"&scope=hubapi"
+
"&username="
+
username
+
"&password="
+
password
Log
.
d
(
"postBody"
,
postBody
)
...
...
@@ -32,8 +34,26 @@ class Authenticate(context: Context) {
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
}
}
fun
refreshAccessToken
(
clientId
:
String
,
clientSecret
:
String
,
refreshToken
:
String
)
{
//val postBody = "grant_type=refresh_token" + "&client_id=" + clientId + "&client_secret=" + clientSecret + "&scope=hubapi" + "&refresh_token=" + refreshToken
val
postBody
=
"grant_type=refresh_token"
.
plus
(
"&client_id="
+
clientId
)
.
plus
(
"&client_secret="
+
clientSecret
)
.
plus
(
"&scope=hubapi"
)
.
plus
(
"&refresh_token="
+
refreshToken
)
Log
.
d
(
"postBody"
,
postBody
)
try
{
postRequest
(
oauth2URL
,
postBody
)
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
}
}
...
...
@@ -74,11 +94,14 @@ class Authenticate(context: Context) {
})
}
internal
fun
saveTheTokens
(
accessToken
:
String
,
refreshToken
:
String
)
{
sharedPrefs
.
accessToken
=
accessToken
sharedPrefs
.
setRefreshToken
(
refreshToken
)
sharedPrefs
.
refreshToken
=
refreshToken
//sharedPrefs.setRefreshToken(refreshToken)
}
internal
fun
checkToken
():
String
{
Log
.
d
(
"checkToken"
,
"was called"
)
val
get
=
sharedPrefs
.
accessToken
...
...
@@ -88,10 +111,10 @@ class Authenticate(context: Context) {
return
sharedPrefs
.
accessToken
}
companion
object
{
val
JSON
=
MediaType
.
parse
(
"application/json; charset=utf-8"
)
val
FORM_ENCODED
=
MediaType
.
parse
(
"application/x-www-form-urlencoded; charset=utf-8"
)
}
}
\ No newline at end of file
app/src/main/java/tech/essensys/operateapiexample/MainActivity.kt
View file @
0e7b90cf
...
...
@@ -6,6 +6,7 @@ import android.support.design.widget.FloatingActionButton
import
android.support.design.widget.Snackbar
import
android.support.v7.app.AppCompatActivity
import
android.support.v7.widget.Toolbar
import
android.util.Log
import
android.view.View
import
android.view.Menu
import
android.view.MenuItem
...
...
@@ -14,6 +15,11 @@ import android.widget.Button
class
MainActivity
:
AppCompatActivity
()
{
private
val
clientId
=
""
private
val
clientSecret
=
""
private
val
userId
=
""
private
val
userPassword
=
""
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
...
...
@@ -26,8 +32,11 @@ class MainActivity : AppCompatActivity() {
.
setAction
(
"Action"
,
null
).
show
()
}
val
button
=
findViewById
(
R
.
id
.
loginButton
)
as
Button
button
.
setOnClickListener
{
login
()
}
val
loginButton
=
findViewById
(
R
.
id
.
loginButton
)
as
Button
loginButton
.
setOnClickListener
{
login
()
}
val
refreshAccessTokenButton
=
findViewById
(
R
.
id
.
refreshAccessTokenButton
)
as
Button
refreshAccessTokenButton
.
setOnClickListener
{
refreshAccessToken
()
}
val
getArea1
=
findViewById
(
R
.
id
.
getArea1
)
as
Button
getArea1
.
setOnClickListener
{
getArea
()
}
...
...
@@ -65,8 +74,16 @@ class MainActivity : AppCompatActivity() {
}
private
fun
login
()
{
Log
.
d
(
"login"
,
"called"
)
val
authenticate
=
Authenticate
(
this
)
authenticate
.
login
(
clientId
,
clientSecret
,
userId
,
userPassword
)
}
private
fun
refreshAccessToken
()
{
val
sharedPrefs
=
SharedPrefs
(
this
)
val
refreshToken
=
sharedPrefs
.
refreshToken
val
authenticate
=
Authenticate
(
this
)
authenticate
.
login
(
"account@domain.com"
,
""
)
authenticate
.
refreshAccessToken
(
clientId
,
clientSecret
,
refreshToken
)
}
private
fun
getArea
()
{
...
...
app/src/main/java/tech/essensys/operateapiexample/OperateAPI1.kt
View file @
0e7b90cf
...
...
@@ -33,6 +33,7 @@ class OperateAPI1(context: Context) {
accessToken
=
sharedPrefs
.
accessToken
}
operator
fun
get
(
endpoint
:
String
,
query
:
String
)
{
try
{
getRequest
(
baseAPI
+
endpoint
+
query
)
...
...
@@ -45,27 +46,28 @@ class OperateAPI1(context: Context) {
fun
put
(
endpoint
:
String
,
data
:
String
)
{
try
{
putRequest
(
baseAPI
+
"account"
,
data
)
putRequest
(
baseAPI
+
endpoint
,
data
)
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
}
}
fun
delete
(
endpoint
:
String
,
item
:
Int
)
{
try
{
deleteRequest
(
"account"
,
item
)
deleteRequest
(
endpoint
,
item
)
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
}
}
@Throws
(
IOException
::
class
)
private
fun
deleteRequest
(
endpoint
:
String
,
item
:
Int
)
{
val
client
=
OkHttpClient
()
val
itemid
=
String
.
format
(
"%d"
,
item
)
val
formBody
=
FormBody
.
Builder
()
...
...
@@ -102,6 +104,7 @@ class OperateAPI1(context: Context) {
})
}
@Throws
(
IOException
::
class
)
private
fun
getRequest
(
getUrl
:
String
)
{
...
...
app/src/main/java/tech/essensys/operateapiexample/SharedPrefs.kt
View file @
0e7b90cf
...
...
@@ -10,18 +10,14 @@ import android.preference.PreferenceManager
*/
class
SharedPrefs
(
context
:
Context
)
:
Activity
()
{
private
val
preferences
:
SharedPreferences
init
{
preferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
context
)
}
fun
setRefreshToken
(
refreshtoken
:
String
)
{
val
editor
=
preferences
.
edit
()
editor
.
putString
(
"refreshToken"
,
refreshtoken
)
editor
.
apply
()
}
var
accessToken
:
String
get
()
=
preferences
.
getString
(
"accessToken"
,
""
)
...
...
@@ -31,6 +27,16 @@ class SharedPrefs(context: Context) : Activity() {
editor
.
apply
()
}
var
refreshToken
:
String
get
()
=
preferences
.
getString
(
"refreshToken"
,
""
)
set
(
refreshToken
)
{
val
editor
=
preferences
.
edit
()
editor
.
putString
(
"refreshToken"
,
refreshToken
)
editor
.
apply
()
}
companion
object
{
private
val
PREFS_NAME
=
"MyPrefsFile"
}
...
...
app/src/main/res/layout/content_main.xml
View file @
0e7b90cf
...
...
@@ -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