Google Cloud Messaging(GCM)与Android上的本地设备组提供HTTP Error code 401 [英] Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401

查看:134
本文介绍了Google Cloud Messaging(GCM)与Android上的本地设备组提供HTTP Error code 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Google云消息传递(GCM)在本地设备组的Android上工作,如 https://developers.google.com/cloud-messaging/android/client-device-group



我已在Web控制台中启用了以下服务( https://console.developers.google.com ):




  • Google云消息传递for Android
  • Google云端发布/订阅

  • Google+ API



我还创建了以下凭据: ul>

  • API密钥(带有指定的软件包名称和SHA-1证书密钥)

  • OAuth 2.0客户端ID(Web应用程序)

    我的Android代码如下所示(错误处理等已被剥离):

     字符串帐户= ACCOUNT_NAME; //例如johndoe@google.com
    String scope =audience:server:client_id:+ WEB_APPLICATION_CLIENT_ID;
    String token = GoogleAuthUtil.getToken(context,account,scope);

    //在此处成功找到代币:-)

    网址=新网址(https://android.googleapis.com/gcm/notification);
    HttpURLConnection连接=(HttpURLConnection)url.openConnection();
    connection.setRequestMethod(POST);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestProperty(project_id,NUMERICAL_PROJECT_ID);
    connection.setRequestProperty(Content-Type,application / json);
    connection.setRequestProperty(Accept,application / json);
    connection.connect();

    JSONObject requestBody = new JSONObject();
    requestBody.put(operation,add);
    requestBody.put(notification_key_name,foobar);
    requestBody.put(registration_ids,
    new JSONArray(Arrays.asList(new String [] {42,44})));
    requestBody.put(id_token,token);

    OutputStream os = connection.getOutputStream();
    os.write(requestBody.toString()。getBytes(UTF-8));
    os.close();

    // connection.getResponseCode()现在是401

    提交的JSON https://android.googleapis.com/gcm/notification 看起来像这样:

      {
    operation:add,
    notification_key_name:foobar,
    registration_ids:[42,44],
    id_token:[veeeeeery long string]
    }

    响应内容为:

     < HTML> 
    < HEAD>
    < TITLE>未经授权的< / title>
    < / HEAD>
    < BODY BGCOLOR =#FFFFFFTEXT =#000000>
    < H1>未经授权< / H1>
    < H2>错误401< / H2>
    < / BODY>
    < / HTML>

    我做错了什么?

    解决方案

    找到诀窍:您正在使用Google帐户来使用id_token,您需要将电子邮件作为notification_key_name使用。因此,如果您使用的是foo@gmail.com,则需要将此地址用作notification_key_name。


    I am trying to get Google Cloud Messaging (GCM) to work on Android with local device groups as described on https://developers.google.com/cloud-messaging/android/client-device-group .

    I have enabled the following services in the web console ( https://console.developers.google.com ):

    • Google Cloud Messaging for Android
    • Google Cloud Pub/Sub
    • Google+ API

    I have also created the following credentials:

    • API key (with specified package name and SHA-1 certificate keyprint)
    • OAuth 2.0 client IDs (Web application)

    My Android code looks as follows (error handling etc. has been stripped):

    String account = ACCOUNT_NAME; // E.g. "johndoe@google.com"
    String scope = "audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID;
    String token = GoogleAuthUtil.getToken(context, account, scope);
    
    // Token is successfully found here :-)
    
    URL url = new URL("https://android.googleapis.com/gcm/notification");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestProperty("project_id", NUMERICAL_PROJECT_ID);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    connection.connect();
    
    JSONObject requestBody = new JSONObject();
    requestBody.put("operation", "add");
    requestBody.put("notification_key_name", "foobar");
    requestBody.put("registration_ids", 
      new JSONArray(Arrays.asList(new String[]{"42", "44"})));
    requestBody.put("id_token", token);
    
    OutputStream os = connection.getOutputStream();
    os.write(requestBody.toString().getBytes("UTF-8"));
    os.close();
    
    // connection.getResponseCode() is now 401
    

    The submitted JSON to https://android.googleapis.com/gcm/notification looks something like this:

    {
      "operation": "add",
      "notification_key_name": "foobar",
      "registration_ids": ["42","44"],
      "id_token": "[veeeeeery long string]"
    }
    

    The response contents is:

    <HTML>
    <HEAD>
    <TITLE>Unauthorized</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Unauthorized</H1>
    <H2>Error 401</H2>
    </BODY>
    </HTML>
    

    What am I doing wrong?

    解决方案

    Found the trick: you are using a google account to take the id_token, you need to use EXACTLY the email as notification_key_name. So if you are using foo@gmail.com, you need to use this address as notification_key_name.

    这篇关于Google Cloud Messaging(GCM)与Android上的本地设备组提供HTTP Error code 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆