1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
//-----------------------------------------------------------------------
// <copyright file="YammerConsumer.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.ApplicationBlock
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages;
public static class YammerConsumer
{
/// <summary>
/// The Consumer to use for accessing Google data APIs.
/// </summary>
public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription {
RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/request_token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest),
UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/authorize", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/access_token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new PlaintextSigningBindingElement() },
ProtocolVersion = ProtocolVersion.V10,
};
public static DesktopConsumer CreateConsumer(IConsumerTokenManager tokenManager) {
return new DesktopConsumer(ServiceDescription, tokenManager);
}
public static Task<Tuple<Uri, string>> PrepareRequestAuthorizationAsync(DesktopConsumer consumer, CancellationToken cancellationToken = default(CancellationToken)) {
if (consumer == null) {
throw new ArgumentNullException("consumer");
}
return consumer.RequestUserAuthorizationAsync(null, null, cancellationToken);
}
public static async Task<AuthorizedTokenResponse> CompleteAuthorizationAsync(DesktopConsumer consumer, string requestToken, string userCode, CancellationToken cancellationToken = default(CancellationToken)) {
// Because Yammer has a proprietary callback_token parameter, and it's passed
// with the message that specifically bans extra arguments being passed, we have
// to cheat by adding the data to the URL itself here.
var customServiceDescription = new ServiceProviderDescription {
RequestTokenEndpoint = ServiceDescription.RequestTokenEndpoint,
UserAuthorizationEndpoint = ServiceDescription.UserAuthorizationEndpoint,
AccessTokenEndpoint = new MessageReceivingEndpoint(ServiceDescription.AccessTokenEndpoint.Location.AbsoluteUri + "?oauth_verifier=" + Uri.EscapeDataString(userCode), HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest),
TamperProtectionElements = ServiceDescription.TamperProtectionElements,
ProtocolVersion = ProtocolVersion.V10,
};
// To use a custom service description we also must create a new WebConsumer.
var customConsumer = new DesktopConsumer(customServiceDescription, consumer.TokenManager);
var response = await customConsumer.ProcessUserAuthorizationAsync(requestToken, userCode, cancellationToken);
return response;
}
}
}
|