无法调用MobileFirst 8.0 PushNotification Rest Endpoint

时间:2017-02-28 16:39:39

标签: ibm-mobilefirst mobilefirst-server

我正在尝试获取授权令牌,以通过MobileFirst Foundation 8.0服务器向移动应用发送推送通知。我已经编写了以下代码来发送请求,但我不确定我在哪里弄乱了,因为我得到的响应不包含令牌:

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));

        }
    }

我尝试了两种不同的发送请求的方式,但我得到的响应是400-Bad Request。

1 个答案:

答案 0 :(得分:0)

尝试将字符串URI_GET_TOKEN =“mfp / api / az / v1 / token”更改为字符串URI_GET_TOKEN =“/ mfp / api / az / v1 / token”