-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYammerOAuthHelper.ps1
68 lines (53 loc) · 2.26 KB
/
YammerOAuthHelper.ps1
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
65
66
67
68
[System.Reflection.Assembly]::LoadWithPartialName("System.Web")
$SleepInterval = 1
Function Get-Code()
{
[OutputType([string])]
param ($urlEndPoint)
$IE = New-Object -ComObject InternetExplorer.Application;
$IE.Navigate($urlEndPoint);
$IE.Visible = $true;
while ($IE.LocationUrl -notmatch "code") {
Write-host ‘Sleeping {0} seconds for access URL’ -f $SleepInterval;
Start-Sleep -Seconds $SleepInterval;
}
[string]$fyi = (($IE.LocationUrl -split ',*code=')[1]);
$IE.Quit();
return $fyi;
}
function Get-YammerToken()
{
#setup the client connection
$clientId = "<REPLACE WITH YOUR YAMMER APP CLIENT ID>"
$clientSecret ="<REPLACE WITH YOUR YAMMER APP CLIENT SECRET>"
$rdurl = "<REPLACE WITH YOUR YAMMER APP REDIRECT URL>"
$redirectUrl = [System.Web.HttpUtility]::UrlEncode($rdurl)
$yammer= "https://www.yammer.com/oauth2/authorize?client_id=$clientId&response_type=code&redirect_uri=$redirectUrl"
$tempCode = [string](Get-Code -urlEndPoint $yammer)
$tempCode = $tempCode.Replace(" ", "")
$yammerLeg2 = "https://www.yammer.com/oauth2/access_token.json?client_id=$clientId&client_secret=$clientSecret&code=$tempCode"
$r = Invoke-WebRequest $yammerLeg2 -Method Get | ConvertFrom-Json
$accessToken = $r.access_token
return $accessToken.token;
}
#Function to add user to a Group
Function Set-YammerGroups()
{
Param ($token, $userId, $groupId, $email)
#configure the Headers for the REST API call
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Bearer '+ $token)
$headers.Add("accept-encoding", "gzip, deflate")
$headers.Add("content-type", "application/json")
#Configure the JSON Object for the push
$body = New-Object PSObject
Add-Member -MemberType NoteProperty -Name group_id -Value $groupId -InputObject $body
Add-Member -MemberType NoteProperty -Name user_id -Value $userId -InputObject $body
Add-Member -MemberType NoteProperty -Name email -Value $email -InputObject $body
$jsonBody =$body|ConvertTo-Json
#the API URL
$groupUrl = "https://www.yammer.com/api/v1/group_memberships.json"
#invoke the call
$answer =Invoke-RestMethod $groupUrl -Headers $headers -Method Post -Body $jsonBody
}
$Global:yammerToken = Get-YammerToken();