Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Function GetAccessToken() As String
- If HttpRuntime.Cache("access_token") Is Nothing Then
- Dim args As Dictionary(Of String, String) = GetOauthTokens(Request.Params("code"))
- HttpRuntime.Cache.Insert("access_token", args("access_token"), Nothing, DateTime.Now.AddMinutes(Convert.ToDouble(args("expires"))), _
- TimeSpan.Zero)
- 'The above doesn't work, as the "expires" argument is no longer available
- End If
- Return String.Empty
- Return HttpRuntime.Cache("access_token").ToString()
- End Function
- Private Function GetOauthTokens(ByVal code As String) As Dictionary(Of String, String)
- Dim Tokens As Dictionary(Of String, String) = New Dictionary(Of String, String)
- Dim ClientID As String = "XXX", RedirectURL As String = "http://localhost/Facebook-Test.aspx", _
- ClientSecret As String = "YYY", _
- Scope As String = "email, publish_stream, offline_access, user_birthday, user_location, friends_location, read_friendlists"
- Dim URL As String = _
- String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",
- ClientID, RedirectURL, ClientSecret, code, Scope)
- Dim Request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
- Using Response As HttpWebResponse = CType(Request.GetResponse(), HttpWebResponse)
- Dim Reader As StreamReader = New StreamReader(Response.GetResponseStream())
- Dim RetVal As String = Reader.ReadToEnd()
- For Each Token As String In RetVal.Split(CChar("&"))
- Tokens.Add(Token.Substring(0, Token.IndexOf("=")), Token.Substring(Token.IndexOf("=") + 1, Token.Length - Token.IndexOf("=") - 1))
- Next
- End Using
- Return Tokens
- End Function
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- If Not String.IsNullOrEmpty(Request.Params("code")) Then
- Dim TokenString As String = String.Empty, FBApp As FacebookAPI, UserDataResult As JSONObject
- FBApp = New FacebookAPI(GetAccessToken())
- 'UserDataResult = CType(FBApp.Get("/me"), JSONObject)
- 'UserDataLiteral.Text = "User Name: " & UserDataResult.Dictionary.Item("name").ToDisplayableString
- End If
- End Sub
- Private Sub LoginLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkButton.Click
- Dim ClientId As String = "XXX"
- Dim RedirectUrl As String = "http://localhost/Facebook-Test.aspx"
- Response.Redirect(String.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", ClientId, RedirectUrl))
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement