Add API method to login (untested)

This commit is contained in:
Jan-Lukas Else 2018-02-12 21:56:36 +01:00
parent e9f035e260
commit d82166935f
1 changed files with 20 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import com.github.kittinunf.fuel.httpPost
import com.github.kittinunf.result.Result
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpCookie
object TelegraphApi {
@ -151,4 +152,23 @@ object TelegraphApi {
}
}
// Telegra.ph
fun login(authUrl: String, callback: (success: Boolean, accessToken: String?, account: Account?) -> Unit) {
authUrl.httpPost().response { _, response, _ ->
var token: String? = null
response.headers["Set-Cookie"]
?.flatMap { HttpCookie.parse(it) }
?.find { it.name == "tph_token" }
?.let {
token = it.value
}
if (token != null) getAccountInfo(accessToken = token!!) { success, account, _ ->
if (success) callback(true, token, account)
else callback(false, null, null)
}
else callback(false, null, null)
}
}
}