2012-03-15 61 views
4

我想使用城市飛艇的試用版對於推送通知
註冊申請頁面的城市飛艇它需要谷歌C2DM授權令牌。但我無法從Google獲取C2DM授權令牌。我已經註冊了我的電子郵件ID與谷歌開始使用C2DM,但他們沒有給我任何授權令牌..如何獲得使用城市飛艇的C2DM授權令牌

我怎麼能從谷歌獲得C2DM授權令牌?

+0

謝謝@OllieC但我接受作爲答案時,因爲其他人搜索相同的問題主要集中在接受的答案上。所以我不能接受那個不正確的答案。謝謝btw – 2012-03-15 11:36:36

回答

4

訪問該網站會給出您的詳細信息,並獲得您的C2DM授權令牌 HURL

0

您必須等待大約24小時才能獲得C2DM授權,有時甚至更多。但是,如果您可以使用curl獲取令牌,則會收到一封確認電子郵件,如文檔中所述。

+0

這是不正確的。首先,C2DM賬戶通常幾乎立即獲得批准。其次,谷歌不會主動發送一個授權令牌,它必須從ClientLogin API請求(並且過期,所以需要重複執行) – 2012-03-15 10:09:22

0

你需要使用谷歌的ClientLogin HTTP API請求的身份驗證令牌,使用您的C2DM帳戶的電子郵件和密碼:

public static String getClientLoginAuthToken() { 
HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin"); 
try { 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
    nameValuePairs.add(new BasicNameValuePair("Email", "C2DMEMAILADDRESS)); 
    nameValuePairs.add(new BasicNameValuePair("Passwd", "C2DMPASSWORD)); 
    nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE")); 
    nameValuePairs.add(new BasicNameValuePair("source", "Google-cURL-Example")); 
    nameValuePairs.add(new BasicNameValuePair("service", "ac2dm")); 
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = client.execute(post); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
    String line = ""; 
    while ((line = rd.readLine()) != null) { 
     Trace.e("HttpResponse", line); 
     if (line.startsWith("Auth=")) { 
      return line.substring(5); 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
Trace.e(TAG, "Failed to get C2DM auth code"); 
return ""; 
} 

有關身份驗證令牌的更多信息,請參見本教程: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html

+0

10我需要在android或服務器端獲得授權令牌嗎? – 2012-03-15 16:18:20

+0

在服務器端。上面的示例代碼是Android代碼 - 我使用它,所以我可以在應用程序中構建一個測試工具來發送推送消息給自己。 – 2012-03-15 16:23:34