您好我试图在我们的应用中获取一些公共驱动器文件,但我无法成功连接到api。
我已按照教程,检查了详细信息,并设法让Android快速入门工作(https://github.com/googledrive/android-quickstart),但我没有连接到api 。
创建了API密钥(多次尝试):
的AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.demo.whatever"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission
android:name="android.permission.GET_ACCOUNTS"
tools:node="remove" />
<uses-permission
android:name="android.permission.USE_CREDENTIALS"
tools:node="remove" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:node="remove" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:node="remove" />
<application
android:name=".EyeSeeTeaApplication"
android:allowBackup="true"
android:icon="@drawable/qualityapp_logo"
android:label="@string/app_name"
android:theme="@style/EyeSeeTheme"
tools:replace="android:icon,android:theme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
应用/的build.gradle :
productFlavors {
eds {
applicationId "org.demo.whatever.eds"
versionName "EDS 2.0.0"
minSdkVersion 15
targetSdkVersion 21
}
hnqis {
applicationId "org.demo.whatever.hnqispull"
versionName "HNQIS 1.0.0"
minSdkVersion 15
targetSdkVersion 21
}
}
标准活动代码:
public class DashboardActivity extends BaseActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
...
@Override
public void onResume(){
Log.d(TAG, "onResume");
super.onResume();
getSurveysFromService();
//FIXME
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client. Once connected, the camera is launched.
mGoogleApiClient.connect();
}
@Override
public void onPause(){
Log.d(TAG, "onPause");
//FIXME
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "API client connected.");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Called whenever the API client fails to connect.
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization
// dialog is displayed to the user.
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
/**
* Handles resolution callbacks.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
//FIXME Always resultCode ==0 instead of -1 (RESULT_OK)
Log.d(TAG,String.format("onActivityResult(%d, %d)",requestCode,resultCode));
}
...
}
问题
我认为包装名称有问题,考虑到有两种口味,但我已经尝试了所有合理的组合,但没有一种是有效的。我已经下载了一个Package Name应用程序,以确保我在API凭据中使用正确的包名称。
任何线索??
答案 0 :(得分:1)
有two essential items for authorization。
- API&amp; auth - &gt;凭证 - &gt;凭证 - &gt; Android客户端1'SHA1'必须与您的'Pakage名称'同步?
- API&amp; auth - &gt;凭证 - &gt; OAuth许可屏幕必须包含您的“产品名称”和“电子邮件地址”
醇>
如果您使用正确的SHA1进行授权,也可以查看应用的keystore。
答案 1 :(得分:0)
为未来的读者。
1-您需要设置OAuth api密钥才能连接客户端。
2-如果你要处理变种,你必须仔细检查 packageName 是 applicationId (如果在 app / build.gradle中指定的话) &gt; productFlavors )。