Log for pixelfed

This commit is contained in:
tom79 2019-04-16 17:07:51 +02:00
parent 9352346ec5
commit f6cf4f8227
5 changed files with 37 additions and 13 deletions

View File

@ -289,6 +289,8 @@ public class LoginActivity extends BaseActivity {
@Override
public void run() {
instanceNodeInfo = new API(LoginActivity.this).getNodeInfo(instance);
Log.v(Helper.TAG,"ins: " +instanceNodeInfo.getName());
runOnUiThread(new Runnable() {
public void run() {
connect_button.setEnabled(true);
@ -297,6 +299,9 @@ public class LoginActivity extends BaseActivity {
case "MASTODON":
socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON;
break;
case "PIXELFED":
socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED;
break;
case "PEERTUBE":
socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE;
break;
@ -304,7 +309,7 @@ public class LoginActivity extends BaseActivity {
socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.GNU;
break;
}
if( instanceNodeInfo.getName().equals("MASTODON")) {
if( instanceNodeInfo.getName().equals("MASTODON") || instanceNodeInfo.getName().equals("PIXELFED")) {
client_id_for_webview = true;
retrievesClientId();
}else {
@ -536,6 +541,12 @@ public class LoginActivity extends BaseActivity {
}else {
parameters.put(Helper.SCOPES, Helper.OAUTH_SCOPES_PEERTUBE);
}
if(socialNetwork == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED){
client_id = "8";
client_secret = "rjnu93kmK1KbRBBMZflMi8rxKJxOjeGtnDUVEUNK";
manageClient(client_id, client_secret, null);
return;
}
parameters.put(Helper.WEBSITE, Helper.WEBSITE_VALUE);
new Thread(new Runnable(){
@ -569,10 +580,7 @@ public class LoginActivity extends BaseActivity {
runOnUiThread(new Runnable() {
public void run() {
client_id = "8";
client_secret = "rjnu93kmK1KbRBBMZflMi8rxKJxOjeGtnDUVEUNK";
manageClient(client_id, client_secret, null);
/*
String message;
if( e.getLocalizedMessage() != null && e.getLocalizedMessage().trim().length() > 0)
message = e.getLocalizedMessage();
@ -580,7 +588,8 @@ public class LoginActivity extends BaseActivity {
message = e.getMessage();
else
message = getString(R.string.client_error);
Toasty.error(getApplicationContext(), message,Toast.LENGTH_LONG).show();*/
Toasty.error(getApplicationContext(), message,Toast.LENGTH_LONG).show();
}
});
}
@ -620,6 +629,8 @@ public class LoginActivity extends BaseActivity {
if( socialNetwork == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
parameters.put("scope", " read write follow");
oauthUrl = "/oauth/token";
}else if( socialNetwork == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
oauthUrl = "/oauth/token";
}else if( socialNetwork == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
parameters.put("scope", "user");
oauthUrl = "/api/v1/users/token";
@ -745,6 +756,8 @@ public class LoginActivity extends BaseActivity {
editor.putString(Helper.ID, id);
editor.apply();
connectionButton.setEnabled(true);
String url2 = redirectUserToAuthorizeAndLogin(socialNetwork, client_id, instance);
Log.v(Helper.TAG,"url: " + url2);
if( client_id_for_webview){
boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true);
if( embedded_browser) {
@ -753,7 +766,7 @@ public class LoginActivity extends BaseActivity {
i.putExtra("instance", instance);
startActivity(i);
}else{
String url = redirectUserToAuthorizeAndLogin(client_id, instance);
String url = redirectUserToAuthorizeAndLogin(socialNetwork, client_id, instance);
Log.v(Helper.TAG,"url: " + url);
Helper.openBrowser(LoginActivity.this, url);
@ -850,11 +863,12 @@ public class LoginActivity extends BaseActivity {
}
public static String redirectUserToAuthorizeAndLogin(String clientId, String instance) {
public static String redirectUserToAuthorizeAndLogin(UpdateAccountInfoAsyncTask.SOCIAL socialNetwork, String clientId, String instance) {
String queryString = Helper.CLIENT_ID + "="+ clientId;
queryString += "&" + Helper.REDIRECT_URI + "="+ Uri.encode(Helper.REDIRECT_CONTENT_WEB);
queryString += "&" + Helper.RESPONSE_TYPE +"=code";
queryString += "&" + Helper.SCOPE +"=" + Helper.OAUTH_SCOPES;
if( socialNetwork != UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED )
queryString += "&" + Helper.SCOPE +"=" + Helper.OAUTH_SCOPES;
return Helper.instanceWithProtocol(instance) + Helper.EP_AUTHORIZE + "?" + queryString;
}

View File

@ -182,7 +182,7 @@ public class WebviewConnectActivity extends BaseActivity {
}
});
webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(clientId, instance));
webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(social, clientId, instance));
}

View File

@ -20,6 +20,7 @@ import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@ -203,11 +204,11 @@ public class API {
response = new HttpsConnection(context).get(nodeInfo.getHref(), 30, null, null);
JSONObject resobj = new JSONObject(response);
JSONObject jsonObject = resobj.getJSONObject("software");
String name = "MASTODON";
String name = jsonObject.getString("name").toUpperCase();
if( jsonObject.getString("name") != null ){
switch (jsonObject.getString("name").toUpperCase()){
case "PEERTUBE":
name = "PEERTUBE";
case "PLEROMA":
name = "MASTODON";
break;
case "HUBZILLA":
case "REDMATRIX":
@ -367,7 +368,9 @@ public class API {
setError(500, new Throwable("An error occured!"));
return null;
}
Log.v(Helper.TAG,"prefKeyOauthTokenT: " + prefKeyOauthTokenT);
String response = new HttpsConnection(context).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT);
Log.v(Helper.TAG,"response! " + response);
account = parseAccountResponse(context, new JSONObject(response));
if( account.getSocial().equals("PLEROMA")){
isPleromaAdmin(account.getAcct());
@ -376,6 +379,8 @@ public class API {
if( e.getStatusCode() == 401 || e.getStatusCode() == 403){
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
if( targetedAccount == null)
return null;
HashMap<String, String> values = refreshToken(targetedAccount.getClient_id(), targetedAccount.getClient_secret(), targetedAccount.getRefresh_token());
if( values.containsKey("access_token") && values.get("access_token") != null) {
targetedAccount.setToken(values.get("access_token"));

View File

@ -154,6 +154,8 @@ public class HttpsConnection {
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Accept", "application/json");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null && !token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
@ -246,6 +248,8 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection)url.openConnection();
httpsURLConnection.setConnectTimeout(30 * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Accept", "application/json");
httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("GET");

View File

@ -201,6 +201,7 @@ public class Helper {
public static final String TAG = "mastodon_etalab";
public static final String CLIENT_NAME_VALUE = "Fedilab";
public static final String OAUTH_SCOPES = "read write follow";
public static final String OAUTH_SCOPES_PIXELFED = "write follow";
public static final String OAUTH_SCOPES_PEERTUBE = "user";
public static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";