Advertisement
Guest User

Untitled

a guest
Dec 4th, 2010
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.81 KB | None | 0 0
  1. Private Function GetAccessToken() As String
  2.  
  3.         If HttpRuntime.Cache("access_token") Is Nothing Then
  4.  
  5.             Dim args As Dictionary(Of String, String) = GetOauthTokens(Request.Params("code"))
  6.  
  7.             HttpRuntime.Cache.Insert("access_token", args("access_token"), Nothing, DateTime.Now.AddMinutes(Convert.ToDouble(args("expires"))), _
  8.             TimeSpan.Zero)
  9.             'The above doesn't work, as the "expires" argument is no longer available
  10.  
  11.         End If
  12.  
  13.         Return String.Empty
  14.  
  15.         Return HttpRuntime.Cache("access_token").ToString()
  16.  
  17.     End Function
  18.  
  19.     Private Function GetOauthTokens(ByVal code As String) As Dictionary(Of String, String)
  20.  
  21.         Dim Tokens As Dictionary(Of String, String) = New Dictionary(Of String, String)
  22.  
  23.         Dim ClientID As String = "XXX", RedirectURL As String = "http://localhost/Facebook-Test.aspx", _
  24.         ClientSecret As String = "YYY", _
  25.         Scope As String = "email, publish_stream, offline_access, user_birthday, user_location, friends_location, read_friendlists"
  26.  
  27.         Dim URL As String = _
  28.         String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",
  29.         ClientID, RedirectURL, ClientSecret, code, Scope)
  30.  
  31.         Dim Request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
  32.         Using Response As HttpWebResponse = CType(Request.GetResponse(), HttpWebResponse)
  33.             Dim Reader As StreamReader = New StreamReader(Response.GetResponseStream())
  34.             Dim RetVal As String = Reader.ReadToEnd()
  35.  
  36.             For Each Token As String In RetVal.Split(CChar("&"))
  37.                 Tokens.Add(Token.Substring(0, Token.IndexOf("=")), Token.Substring(Token.IndexOf("=") + 1, Token.Length - Token.IndexOf("=") - 1))
  38.             Next
  39.  
  40.         End Using
  41.  
  42.         Return Tokens
  43.  
  44.     End Function
  45.  
  46.  
  47.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  48.  
  49.         If Not String.IsNullOrEmpty(Request.Params("code")) Then
  50.  
  51.             Dim TokenString As String = String.Empty, FBApp As FacebookAPI, UserDataResult As JSONObject
  52.  
  53.             FBApp = New FacebookAPI(GetAccessToken())
  54.  
  55.             'UserDataResult = CType(FBApp.Get("/me"), JSONObject)
  56.  
  57.             'UserDataLiteral.Text = "User Name: " & UserDataResult.Dictionary.Item("name").ToDisplayableString
  58.  
  59.  
  60.         End If
  61.  
  62.  
  63.     End Sub
  64.  
  65.     Private Sub LoginLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkButton.Click
  66.  
  67.         Dim ClientId As String = "XXX"
  68.         Dim RedirectUrl As String = "http://localhost/Facebook-Test.aspx"
  69.  
  70.         Response.Redirect(String.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", ClientId, RedirectUrl))
  71.  
  72.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement