android - 无法使用我的服务对象连接到我的谷歌端点,oauth 2.0 获取 IOException : NetworkError

标签 android google-app-engine google-cloud-storage google-cloud-endpoints google-identity

我已经使用以下链接设置了我的项目 calling from androidadd autherisation backend .我仔细检查了我的凭据和 SHA1 key 。

但是还是报错

    java.io.IOException: NetworkError
    at com.google.android.gms.auth.zzd$1.zzbr(Unknown Source)
    at com.google.android.gms.auth.zzd$1.zzbs(Unknown Source)
    at com.google.android.gms.auth.zzd.zza(Unknown Source)
    at com.google.android.gms.auth.zzd.zzc(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.shantanu.hellocloudendpoints.EndPointAsyncTask.doInBackground(EndPointAsyncTask.java:53)
    at com.shantanu.hellocloudendpoints.EndPointAsyncTask.doInBackground(EndPointAsyncTask.java:22)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)

下面是我的代码:

    public class MainActivity extends AppCompatActivity {
        private static final int REQUEST_ACCOUNT_PICKER = 2;
        private SharedPreferences settings;
        private String accountName;
        private GoogleAccountCredential credential;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            settings = getSharedPreferences("FamousQuotesAndroid1", 0);
            credential = GoogleAccountCredential.usingAudience(this, "server:client_id:xxxxxxxxx.apps.googleusercontent.com");
            setAccountName(settings.getString("ACCOUNT_NAME", null));
            if (credential.getSelectedAccountName() == null) {
                chooseAccount();
            }
        }

        private void setAccountName(String accountName) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("ACCOUNT_NAME", accountName);
            editor.commit();
            credential.setSelectedAccountName(accountName);
            this.accountName = accountName;
        }

        void chooseAccount() {
            startActivityForResult(credential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode,
                                        Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case REQUEST_ACCOUNT_PICKER:
                    if (data != null && data.getExtras() != null) {
                        String accountName =
                                data.getExtras().getString(
                                        AccountManager.KEY_ACCOUNT_NAME);
                        if (accountName != null) {
                            setAccountName(accountName);
                        }
                    }
                    break;

            }
        }

        public void getQuotes(View v) {
            new EndPointAsyncTask(this).execute();
        }

        public GoogleAccountCredential getCredential() {
            return credential;
        }
    }





 @Override
    protected List<Quote> doInBackground(Void... params) {
        if (myApiService == null) { // Only do this once
            QuoteApi.Builder builder = new
                    QuoteApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), context.getCredential()).setRootUrl("https://xxxxx.appspot.com/_ah/api/");
            builder.setApplicationName(context.getPackageName());
            myApiService = builder.build();
        }
        try {
            return myApiService.listQuote().execute().getItems();
        } catch (IOException e) {
            e.printStackTrace();
            return Collections.EMPTY_LIST;
        }
    }

端点方法

@ApiMethod(name = "listQuote", scopes = {Constants.EMAIL_SCOPE},
            clientIds = {Constants.WEB_CLIENT_ID,
                    Constants.ANDROID_CLIENT_ID,
                    com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
            audiences = {Constants.ANDROID_AUDIENCE})
    public CollectionResponse<Quote> listQuote(@Nullable @Named("cursor") String cursorString,
                                               @Nullable @Named("count") Integer count, User user) throws UnauthorizedException {
        if (user == null) throw new UnauthorizedException("User is Not Valid");
        Query<Quote> query = ofy().load().type(Quote.class);
        if (count != null) query.limit(count);
        if (cursorString != null && cursorString != "") {
            query = query.startAt(Cursor.fromWebSafeString(cursorString));
        }
        List<Quote> records = new ArrayList<Quote>();
        QueryResultIterator<Quote> iterator = query.iterator();
        int num = 0;
        while (iterator.hasNext()) {
            records.add(iterator.next());
            if (count != null) {
                num++;
                if (num == count) break;
            }
        }
//Find the next cursor
        if (cursorString != null && cursorString != "") {
            Cursor cursor = iterator.getCursor();
            if (cursor != null) {
                cursorString = cursor.toWebSafeString();
            }
        }
        return CollectionResponse.<Quote>builder().setItems(records).setNextPageToken(cursorString).build();
    }

最佳答案

终于找到问题了。我用于测试的设备的背景数据仅限于 google play 服务。 删除后台数据限制后,它就开始工作了。

关于android - 无法使用我的服务对象连接到我的谷歌端点,oauth 2.0 获取 IOException : NetworkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277898/

相关文章:

python - Google 数据存储 ndb.AND 与 and

python - 使用 Django 将文件发送到 GCS 的最佳方式

java - com.google.api.client 库中的 NoSuchMethodError

java - Android如何为计算数字总和的程序设置结果的最大限制?

android - 更改菜单中的可绘制颜色

google-app-engine - 查询相同类型的根实体和子实体时,datastore 是否需要指定祖先?

ios - 在带有 Google Cloud Endpoints 客户端的 iOS Swift 项目中使用 Google API 客户端库

android - 通过头部跟踪移动 android 鼠标指针

java - ListView 分隔符未分隔项目

java - 尝试获取 Google 日历服务时出错