Hello,
I am making new extension to create google OAuth token to call google API.
In java it works fine. it makes token well.
and I could build the extension successfully with ANT.
but when I run this extension, runtime error occured.
java.lang.NoSuchMethodError: No static method fromStream(Ljava/io/InputStream;)Lcom/google/api/client/googleapis/auth/oauth2/GoogleCredential; in class Lcom/google/api/client/googleapis/auth/oauth2/GoogleCredential; or its super classes (declaration of ‘com.google.api.client.googleapis.auth.oauth2.GoogleCredential’ appears in /data/app/edu.mit.appinventor.aicompanion3-km_HupvZItefA–SrsXeJQ==/base.apk:classes2.dex)
full java source code is here.
package hellosoft.GoogleAccessToken;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.*;
//new
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import android.util.Log;
@DesignerComponent(version = GoogleAccessToken.VERSION,
description = "Get google access token " + “by service account key file”,
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = “”)
@SimpleObject(external = true)
public class GoogleAccessToken extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 1;
private String keyfilepath = "";
private ComponentContainer container;
public GoogleAccessToken(ComponentContainer container) {
super(container.$form());
this.container = container;
}
// Call Block
@SimpleFunction(description = "Make Google Token.")
public static String getAccessToken(String keyfilepath) {
FileInputStream serviceAccount;
String token = "Error";
try {
serviceAccount = new FileInputStream(keyfilepath);
GoogleCredential googleCred = GoogleCredential.fromStream(serviceAccount);
GoogleCredential scoped = googleCred.createScoped(
Arrays.asList(
"https://www.googleapis.com/auth/cloud-platform"
)
);
scoped.refreshToken();
token = scoped.getAccessToken();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
token = "file not found " + keyfilepath;
Log.e("GoogleAccessToken", "File not found " + keyfilepath);
} catch (IOException e) {
// TODO Auto-generated catch block
token = "IOexception";
Log.e("GoogleAccessToken", "IOException ");
}
return token;
}
}
appinventor script is here.
error message is here.
I included all of google libraries.
I guess two things.
- make method static
- libraries version
Please help me.