microsoftgraph / msgraph-sdk-android

Microsoft Graph SDK for Android! https://graph.microsoft.io
Other
51 stars 43 forks source link

Events subscription via webhooks #41

Closed Surbhidhingra closed 7 years ago

Surbhidhingra commented 7 years ago

Hi,

I am implementing events subscription using MS graph android SDK. Below is the code.

IGraphServiceClient mGraphServiceClient = GraphServiceClientManager.getInstance(context).getGraphServiceClient(); mGraphServiceClient.getSubscriptions().buildRequest().post(getSubscription(email, expirationDate), subscriptionICallback);

private Subscription getSubscription(String email, Calendar expirationDate) {
        Subscription subscription = new Subscription();
        subscription.expirationDateTime = expirationDate;
        subscription.notificationUrl = "<Webhook link>";
        subscription.changeType = "created,updated";
        subscription.resource = "me/events";
        subscription.clientState = Base64.encodeToString(email.getBytes(), Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);

        return subscription;
    }

The request body generated through this always throws the error:

Error during http request Error code: InvalidRequest
 Error message: Could not process subscription creation payload. Are all property names spelled and camelCased properly?
POST https://graph.microsoft.com/v1.0/subscriptions
SdkVersion : graph-android-v1.0.0
Authorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...]
{"@odata.context":"https://graph.microsoft.com/v1.[...]
400 : Bad Request
E/DefaultHttpProvider[sendRequestInternal] - 286: [...]
E/DefaultHttpProvider[sendRequestInternal] - 286: [Some information was truncated for brevity, enable debug logging for more details]
E/DefaultHttpProvider[sendRequestInternal] - 286: Throwable detail: 
                                                                                                 com.microsoft.graph.http.GraphServiceException: Error code: InvalidRequest
                                                                                                 Error message: Could not process subscription creation payload. Are all property names spelled and camelCased properly?

                                                                                                 POST https://graph.microsoft.com/v1.0/subscriptions
                                                                                                 SdkVersion : graph-android-v1.0.0
                                                                                                 Authorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...]
                                                                                                 {"@odata.context":"https://graph.microsoft.com/v1.[...]

                                                                                                 400 : Bad Request
                                                                                                 [...]

                                                                                                 [Some information was truncated for brevity, enable debug logging for more details]
                                                                                                     at com.microsoft.graph.http.DefaultHttpProvider.handleErrorResponse(DefaultHttpProvider.java:310)
                                                                                                     at com.microsoft.graph.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:246)
                                                                                                     at com.microsoft.graph.http.DefaultHttpProvider.access$000(DefaultHttpProvider.java:47)
                                                                                                     at com.microsoft.graph.http.DefaultHttpProvider$1.run(DefaultHttpProvider.java:129)
                                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                                     at java.lang.Thread.run(Thread.java:818)
E/agenday.com.pgi.agenday.ui.BaseActivity: create Subscription exceptionError code: InvalidRequest
                                                                                          Error message: Could not process subscription creation payload. Are all property names spelled and camelCased properly?

                                                                                          POST https://graph.microsoft.com/v1.0/subscriptions
                                                                                          SdkVersion : graph-android-v1.0.0
                                                                                          Authorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...]
                                                                                          {"@odata.context":"https://graph.microsoft.com/v1.[...]

                                                                                          400 : Bad Request
                                                                                          [...]

                                                                                          [Some information was truncated for brevity, enable debug logging for more details]

but the same request body is working fine in graph explorer, I have tried many things now but unable to resolve it. Please help me how to work for subscription in android as I can't find any resources for this online.

It's urgent, appreciate your help. Thanks!

Surbhidhingra commented 7 years ago

The issue was due to the BaseSubscription model in Microsoft graph SDK where it is setting "@odata.type":"microsoft.graph.subscription" by default and subscription api is not supporting this in request body and throwing the error: 400

So I resolved it by explicitly making "@odata.type" to null in the subscription model and pass it to post request which worked successfully for me.

Thanks!

iambmelt commented 7 years ago

This issue was resolved by e63e212f8103315f776892331e3702a61c9226e9 in versions 1.1.0+