Access token

Access token

The access token is required to work with API.

Access token you can found in the property AccessTokenValue of the AuthorizationResult.

For example:

CopyC#
var result = OAuthWeb.VerifyAuthorization("result url");
if (result.IsSuccessfully)
{
  // ..successfully.. 
  Console.WriteLine("Access token is: {0}", result.AccessTokenValue);
}
CopyVB
Dim result As AuthorizationResult = OAuthWeb.VerifyAuthorization("result url")
If result.IsSuccessfully Then 
  ' ..successfully..
  Console.WriteLine("Access token is: {0}", result.AccessTokenValue)
End If

In the library is implemented request of the user data via the API provider. You can see it in your code (GetUserInfo method of the clients class).

If you need to work with other methods of the API provider, then you need to request for permission, prior to receipt of the access token. Use the Scope property for this.

For example:

CopyC#
OAuthManager.RegisterClient
(
  new FacebookClient
  (
    "1435890426686808",
    "c6057dfae399beee9e8dc46a4182e8fd"
  )
  { 
    Scope = "public_profile,email,user_friends"
  }
);
CopyVB
OAuthManager.RegisterClient _
(
  New FacebookClient _
  (
    "1435890426686808",
    "c6057dfae399beee9e8dc46a4182e8fd"
  ) With _
  { 
    .Scope = "public_profile,email,user_friends"
  }
)

NOTE: When you change the Scope properties do not forget to request the permissions to obtain information from the user profile.

Information about the permissions you can find the documentation on the providers website.

You can use Nemiro.OAuth.Helpers.ExecuteRequest method for requests to API.

For example:

CopyC#
// get user friends
// query parameters
var parameters = new NameValueCollection
{ 
  { "access_token", accessToken },
  { "count", "10" }, // first 10 friends
  { "fields", "nickname" }
};

// execute the request
var result = Nemiro.OAuth.Helpers.ExecuteRequest
(
  "GET",
  "https://api.vk.com/method/friends.get",
  parameters,
  null
);

foreach (Dictionary<string, object> itm in (Array)result["response"])
{
  string friendName = "";
  if (itm.ContainsKey("first_name") && itm.ContainsKey("last_name"))
  {
    friendName = String.Format("{0} {1}", itm["first_name"], itm["last_name"]);
  }
  else
  {
    friendName = itm["nickname"].ToString();
  }
  Console.WriteLine(friendName);
}
CopyVB
' get user friends
' query parameters
Dim parameters As New NameValueCollection() From _
{ 
  { "access_token", accessToken },
  { "count", "10" },
  { "fields", "nickname" }
}

' execute the request
Dim result As RequestResult = Helpers.ExecuteRequest _
(
  "GET",
  "https://api.vk.com/method/friends.get",
  parameters,
  Nothing
)

For Each itm As Dictionary(Of String, Object) In CType(result("response"), Array)
  Dim friendName As String = ""
  If itm.ContainsKey("first_name") AndAlso itm.ContainsKey("last_name") Then
    friendName = String.Format("{0} {1}", itm("first_name"), itm("last_name"))
  Else
    friendName = itm("nickname").ToString()
  End If
  Console.WriteLine(friendName)
Next

NOTE: Please, do not add the code to work with the API to the library. Creat for API individual projects. The Nemiro.OAuth library is intended for exclusive OAuth (not for providers API).

See Also

Scope
Parameters