401 Unauthorized - 请求未到达端点

401 Unauthorized - Request not reaching the endpoint

我无法从我的 Android 应用程序访问我的 Google Cloud Endpoints 后端。我使用以下相关工具和库:

我已经按照有关使用物理设备针对本地开发服务器进行测试的文档进行操作,这意味着我已经将 httpAddress = "0.0.0.0" 行添加到后端模块中的 build.gradle 行,并且我使用以下代码连接到后端:

SomeApi.Builder builder = new SomeApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
builder.setApplicationName("someapplicationname");
builder.setRootUrl("http://192.168.1.1:8080/_ah/api/");
builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer()
{
    @Override
    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException
    {
        abstractGoogleClientRequest.setDisableGZipContent(true);
    }
});
SomeApi api = builder.build();

然后我使用以下行调用创建的 api 的方法:

api.someMethod("Some random string").execute();

这就是问题所在。此方法调用触发以下指向同一方法调用(上面的行)的异常:

W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.interceptResponse(AbstractGoogleClientRequest.java:321)
W/System.err:     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
W/System.err:     at se.essvagsystemab.mobiltdispenssystem.licensemanager.simple.fragment.MainFragment.doInBackground(MainFragment.java:381)
W/System.err:     at se.essvagsystemab.mobiltdispenssystem.licensemanager.simple.fragment.MainFragment.doInBackground(MainFragment.java:373)
W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:304)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:761)

奇怪的是请求甚至没有到达端点。异常似乎是由客户端端点库抛出的,因为服务器日志没有显示任何错误迹象,它包含启动服务器时打印的日志消息,仅此而已。

我不知道是什么原因造成的,非常感谢您的帮助!

原来我从 ipconfig- 输出中复制了错误的 IP 地址,尽管我提到我使用的是正确的 IP 地址。哼哼,尴尬...

在尝试(感觉上)所有可能的解决方案后,我再次检查了 IP,结果发现我复制了标有 Default Gateway 的 IP,这显然不如标有 IPv4 Address!

的那个正确

因此,如果您遇到同样的问题,但仍然觉得自己已正确完成所有操作,请确保在放弃之前仔细检查 IP!