Commit 0e7b90cf authored by Paul Bird's avatar Paul Bird

Add refresh access token

parent afadd295
......@@ -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">
......
......@@ -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
......@@ -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() {
......
......@@ -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) {
......
......@@ -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"
}
......
......@@ -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"
app:layout_constraintBottom_toBottomOf="parent"
android: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"
app:layout_constraintBottom_toBottomOf="parent"
android: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>
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