2017-02-28 69 views
0

我正試圖通過MobileFirst Foundation 8.0服務器獲取授權令牌以將推送通知發送到移動應用程序。我寫了下面的代碼發送請求,但我不知道我在哪裏搞亂的東西了,因爲它我得到響應不包含令牌:無法調用MobileFirst 8.0 PushNotification休息端點

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpHost; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.wink.client.ClientConfig; 
import org.apache.wink.client.ClientResponse; 
import org.apache.wink.client.Resource; 
import org.apache.wink.client.RestClient; 
    public class SendPushNotification { 
     private String MFP_SERVER_URL = "1.2.3.4"; 
     private int MFP_SERVER_PORT = 1234; 
     private static String URI_GET_TOKEN = "mfp/api/az/v1/token"; 
     private static String URI_SEND_MESSAGE = "imfpush/v1/apps/my.app/messages"; 
     private HttpClient client = new DefaultHttpClient(); 
     private HttpHost host = new HttpHost(MFP_SERVER_URL,MFP_SERVER_PORT,"http");; 
     private HttpPost post; 
     public HttpResponse invokeRestfulService(String uri) throws ClientProtocolException, IOException{ 
     List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>(); 
     parametersBody.add(new BasicNameValuePair("grant_type","client_credentials")); 
     parametersBody.add(new BasicNameValuePair("scope","access.sendMessage push.application.my.app messages.write")); 

     post = new HttpPost(uri); 
     post.setHeader("Authorization", "Basic Z292Lm1zLmFjY2Vzcy5wdXNoY2xpZW50OjJmaWxldWkudWF0"); 
     post.setHeader("Content-type", "application/x-www-form-urlencoded"); 
     post.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8)); 

     return client.execute(host,post); 
     } 

     public static void main(String[] args) throws ClientProtocolException, IOException{ 
     SendPushNotification notification = new SendPushNotification(); 
     HttpResponse httpResponse = notification.invokeRestfulService(URI_GET_TOKEN); 
     System.out.println(httpResponse); 
     notification.restClient(); 
     } 

     public void restClient(){ 
     ClientConfig config = new ClientConfig(); 
     config.setBypassHostnameVerification(true); 
     RestClient restClient = new RestClient(config); 

     Resource unprotectedResource = restClient.resource("http://"+MFP_SERVER_URL + ":" + MFP_SERVER_PORT); 
     unprotectedResource.header("Content-Type", "application/x-www-form-urlencoded"); 
     unprotectedResource.header("Authorization", "Basic Z292Lm1zLmFjY2Vzcy5wdXNoY2xpZW50OjJmaWxldWkudWF0"); 

     unprotectedResource.attribute("grant_type", "client_credentials"); 
     unprotectedResource.attribute("scope", "access.sendMessage push.application.gov.ms.tofileui messages.write"); 

     ClientResponse clientResponse = unprotectedResource.contentType("application/x-www-form-urlencoded").accept("*/*").post(URI_GET_TOKEN); 

     System.out.println(clientResponse.getEntity(String.class)); 

     } 
    } 

我已經試過2種不同的發送方式請求但我得到的迴應是400-Bad Request。

+0

您好Prera​​k。就像你最近的其他相關文章一樣,在這裏跟蹤會很有幫助。請附上它,這將幫助我們確定問題可能是什麼。謝謝! –

回答

0

嘗試改變字符串URI_GET_TOKEN = 「MFP/API/AZ/V1 /令牌」 爲字符串URI_GET_TOKEN = 「/ MFP/API/AZ/V1 /令牌」

相關問題