我想从我的Android设备上使用Google存储空间。我启用了谷歌存储服务和谷歌存储json api。我也启用了结算。
我从以下命令行获取了SHA1指纹:
keytool -list -v -keystore ~/.android/debug.keystore
我在api控制台中设置了“SHA1 key; com.mydomain”,我得到了我的Simple API。
我正在尝试获取存储桶的内容,但我遇到了以下错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1042)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.zenittude.image.importer.GoogleCloudStorageImageImporter.getBucket(GoogleCloudStorageImageImporter.java:183)
at com.zenittude.image.importer.GoogleCloudStorageImageImporter.access$300(GoogleCloudStorageImageImporter.java:61)
at com.zenittude.image.importer.GoogleCloudStorageImageImporter$BucketTask.doInBackground(GoogleCloudStorageImageImporter.java:212)
at com.zenittude.image.importer.GoogleCloudStorageImageImporter$BucketTask.doInBackground(GoogleCloudStorageImageImporter.java:194)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
这是我的班级:
@EBean
public class GoogleCloudStorageImageImporter implements IImageImporter {
public static final String TAG = "GoogleCloudStorageImageImporter";
private static final String APPLICATION_NAME = "MyCompany-MyApp/1.0";
private final JsonFactory JSON_FACTORY = new JacksonFactory();
private HttpTransport transport = AndroidHttp.newCompatibleTransport();
private GoogleCloudSettings settings;
private Storage storage;
@RootContext
Activity activity;
@RootContext
Context context;
@Bean(DefaultImageManager.class)
IImageManager imageManager;
@Bean(OrmliteRepositoryManager.class)
ILocalRepository repository;
Account account;
GoogleAccountCredential credential;
private void readSettings() {
settings = GoogleCloudSettings.load(JSON_FACTORY,
GoogleCloudSettings.class.getResourceAsStream("/foo_settings.json"));
settings.setBucket("client-" + MainApplication.getBusinessId());
}
private Storage getStorageService() {
StorageRequestInitializer storageRequestInitializer = new StorageRequestInitializer(ClientCredentials.API_KEY);
return new Storage.Builder(transport,
JSON_FACTORY,
credential)
.setApplicationName(APPLICATION_NAME)
.setStorageRequestInitializer(storageRequestInitializer)
.build();
}
public void doImport() {
try {
// settings
readSettings();
if( account == null) {
GoogleAccountManager googleAccountManager = new GoogleAccountManager(activity);
Account[] accounts = googleAccountManager.getAccountManager().getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
account = accounts[2];
}
new BucketTask().execute(account);
} catch (Throwable t) {
Log.e(TAG, "Error while trying to import images from server", t);
}
}
private Bucket getBucket() {
Log.i("Getting bucket " + settings.getBucket() + " metadata");
Bucket bucket = null;
try {
Storage.Buckets.Get getBucket = storage.buckets().get(settings.getBucket());
getBucket.setPrettyPrint(true);
bucket = getBucket.execute();
Log.i(bucket.toPrettyString());
} catch (UserRecoverableAuthIOException e) {
activity.startActivityForResult(e.getIntent(), StartupActivity.REQUEST_AUTHORIZATION);
} catch (IOException e) {
Log.e(TAG, "unable to get the bucket", e);
}
return bucket;
}
private class BucketTask extends AsyncTask<Account, Void, Bucket> {
@Override
protected Bucket doInBackground(Account... params) {
try {
if(credential == null) {
Account account = params[0];
credential = GoogleAccountCredential.usingOAuth2(activity, Collections.singleton(StorageScopes.DEVSTORAGE_READ_ONLY));
credential.setSelectedAccountName(account.name);
}
if( storage == null && credential != null) {
storage = getStorageService();
}
return getBucket();
} catch (Throwable t) {
Log.e(TAG, "Error while trying to import images from server", t);
}
return null;
}
@Override
protected void onPostExecute(Bucket result) {
//Do something with result
if (result != null)
result.getId();
}
}
}
我关注了Drive样本。
有什么建议吗?
答案 0 :(得分:1)
我发现了这个问题。
请勿使用来自API控制台的简单API访问,但您必须为已安装的应用程序设置客户端ID (请参阅上面的Drive SDK链接)< / p>
所以而不是:
private Storage getStorageService() {
StorageRequestInitializer storageRequestInitializer = new StorageRequestInitializer(ClientCredentials.API_KEY);
return new Storage.Builder(transport,
JSON_FACTORY,
credential)
.setApplicationName(APPLICATION_NAME)
.setStorageRequestInitializer(storageRequestInitializer)
.build();
}
你应该像这样使用它:
private Storage getStorageService() {
return new Storage.Builder(transport,
JSON_FACTORY,
credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
希望有所帮助!