我尝试为blogger调整task-android-sample但是它返回了这个错误:
logcat的 04-23 13:28:21.949:W / System.err(2286):com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 04-23 13:28:21.969:W / System.err(2286):at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential $ RequestHandler.intercept(GoogleAccountCredential.java:286) 04-23 13:28:21.969:W / System.err(2286):at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
在console.developers.google.com中,我激活了BloggerV3并为我的应用程序创建了一个OAuth凭证,其中包含了我的应用程序的包名称和良好的sha1。
有什么问题? 谢谢
以下是代码:
public class MainActivity extends Activity {
private static final Level LOGGING_LEVEL = Level.OFF;
private static final String PREF_ACCOUNT_NAME = "accountName";
GoogleAccountCredential credential;
final HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
static final int REQUEST_GOOGLE_PLAY_SERVICES = 0;
static final int REQUEST_AUTHORIZATION = 1;
static final int REQUEST_ACCOUNT_PICKER = 2;
Blogger blogger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.getLogger("com.google.api.client").setLevel(LOGGING_LEVEL);
setContentView(R.layout.activity_main);
credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(BloggerScopes.BLOGGER));//"https://www.googleapis.com/auth/blogger"));
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// Tasks client
blogger =
new com.google.api.services.blogger.Blogger.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("MyBlogger/1.0").build();
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
protected void onResume() {
super.onResume();
if (checkGooglePlayServicesAvailable()) {
haveGooglePlayServices();
}
}
void showGooglePlayServicesAvailabilityErrorDialog(final int connectionStatusCode) {
runOnUiThread(new Runnable() {
public void run() {
Dialog dialog =
GooglePlayServicesUtil.getErrorDialog(connectionStatusCode, MainActivity.this,
REQUEST_GOOGLE_PLAY_SERVICES);
dialog.show();
}
});
}
/** Check that Google Play services APK is installed and up to date. */
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
private void haveGooglePlayServices() {
// check if there is already an account selected
if (credential.getSelectedAccountName() == null) {
// ask user to choose account
chooseAccount();
} else {
// blogger post
BloggerTest();
}
}
private void BloggerTest() {
Post npost = new Post();
npost.setContent("coucou");
npost.setTitle("title");
(new AsyncLoadInsertPost(this)).execute(npost);
}
public class AsyncLoadInsertPost extends AsyncTask<Post, Void, Post> {
// Store the context passed to the AsyncTask when the system instantiates it.
Context localContext;
// Constructor called by the system to instantiate the task
public AsyncLoadInsertPost(Context context) {
// Required by the semantics of AsyncTask
super();
// Set a Context for the background task
localContext = context;
}
@Override
protected Post doInBackground(Post... params) {
Post post = null;
Post npost = params[0];
try {
Insert postsInsertAction = blogger.posts().insert("XXXXXXXXXXXXXXXXXXXXX", npost)
.setIsDraft(true);
post = postsInsertAction.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return post;
}
}
private void chooseAccount() {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
答案 0 :(得分:0)