0
我正在整合Slack Api: Sign in with Slack
。我從Slack Api
有幾件事。使用Slack Api登錄,Android
- 客戶ID: '東西'
- 客戶祕密: '東西'
- 驗證網址:https://slack.com/oauth/authorize
- 令牌網址:https://slack.com/api/oauth.access
- 範圍:識別
我需要的code
參數。
在我的Activity
班中,我爲此使用WebView
。要訪問code
參數,在shouldOverrideUrlLoading
方法我用:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://localhost:8000/")) {
UrlQuerySanitizer urlQuerySanitizer = new UrlQuerySanitizer(url);
String CLIENT_CODE = urlQuerySanitizer.getValue("code");
Log.e(TAG, "shouldOverrideUrlLoading: "+ CLIENT_CODE);
loginPresenter.loginSuccess();
return true;
} else {
view.loadUrl(url);
return true;
}
}
我做類叫HttpConstant
保持網址:
public class HttpsConstant {
public static final String AUTHORIZATION_SLACK_URL = "https://slack.com/oauth/authorize?scope=identity.basic,identity.email,identity.avatar&client_id=something";
public static final String TOKEN_URL = "https://slack.com/";
}
我做類叫StrngConstant
保持這樣:CLIENT_ID, CLIENT_SECRET, CLIENT_CODE
。
public final class StringConstant {
public static final String CLIENT_ID = "something";
public static final String CLIENT_SECRET = "something" ;
public static final String CLIENT_CODE = "something but still does not
able to pass the code parameter here from my activity class";
}
我使用Retrofit
爲登錄服務:
public interface LoginService {
@GET("api/oauth.access?client_id=" + StringConstant.CLIENT_ID +
"&client_secret=" + StringConstant.CLIENT_SECRET +
"&code="+ StringConstant.CLIENT_CODE)
Call<LoginResponse> getLoginToken();
}
我已經在shouldOverrideUrl
方法使用SharedPreferences
但無法使用嘗試。
SharedPreferences preferences = getSharedPreferences("ClientCode", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Code", CLIENT_CODE);
editor.commit();
如何在登錄服務中通過code
參數?我怎樣才能使用這個SharedPreferences
,所以我可以訪問code
參數?或者,有沒有其他方法可以獲得code
參數並使用它?
在這個LoginService類中; getSharedPreferences無法解析,它也這樣說。 –
接口是爲聲明而不是爲細節的工具。所以使用活動或片段或靜態函數Contex –
這就是爲什麼我使用StringConstant類。 –