実現したいこと
androidアプリからブラウザを立ち上げmastodon認証画面に飛び承認をタップさせてアプリに戻ってくるようにしたい
やっていること
client_idとclient_secretを取得するところまではできました。それらを使ってアプリからブラウザを起動させ、
リダイレクトにuri schemeを指定しアプリに戻ってくるという動作を実装しようとしています。
発生してる問題
ブラウザから/oauth/authorizeを叩くと「無効なリダイレクトURIが含まれています」と返ってきます。
試したこと
redirect_uriを"urn:ietf:wg:oauth:2.0:oob"にしたらトークンが表示されました。
ソースと設定ファイル
kotlin
1inner class UserLoginTask internal constructor(private val mEmail: String) : AsyncTask<Void, Void, Boolean>() { 2 override fun doInBackground(vararg params: Void): Boolean? { 3 // client_idとclient_secretを取得 4 } 5 override fun onPostExecute(success: Boolean?) { 6 showProgress(false) 7 if (success) { 8 var url = mDomain + "/oauth/authorize?" + 9 Uri.encode("scope") + "=" + Uri.encode("read write follow") + "&" + 10 Uri.encode("client_id") + "=" + Uri.encode(mClientId) + "&" + 11 Uri.encode("redirect_uri") + "=" + Uri.encode(getString(R.string.redirect_scheme) + "://" + getString(R.string.redirect_host)) + "&" + 12 Uri.encode("response_type") + "=" + Uri.encode("code") 13 var uri = Uri.parse(url) 14 Log.d(getString(R.string.print_tag), uri.toString()) 15 var i = Intent(Intent.ACTION_VIEW,uri) 16 startActivity(i) 17 } 18 else { 19 mInstanceView!!.error = getString(R.string.error_invalid_instance) 20 mInstanceView!!.requestFocus() 21 } 22 } 23}
Androidmanifest.xmlはこうなっています。
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="jp.hoge.hoge"> 4 5 <uses-permission android:name="android.permission.INTERNET" /> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/AppTheme"> 13 <activity 14 android:name=".SigninActivity"> 15 <intent-filter> 16 <action android:name="android.intent.action.VIEW" /> 17 <category android:name="android.intent.category.DEFAULT" /> 18 <category android:name="android.intent.category.BROWSABLE" /> 19 <data 20 android:scheme="@string/redirect_scheme" 21 android:host="@string/redirect_host" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26</manifest>
strings.xml
xml
1 <string name="redirect_scheme">scheme</string> 2 <string name="redirect_host">host</string>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。