简体   繁体   English

如何从API(C#Xamarin Studio)保存来自字符串的accessstoken

[英]How to save accesstoken to string from API (C# Xamarin Studio)

I just started learning programming and I am currently trying out developing an android mobile application. 我刚刚开始学习编程,我正在尝试开发一个Android移动应用程序。 I have watched a lot of tutorials and trying to find solutions online, but i can't seem to get a hang of it. 我已经看了很多教程,并试图在网上找到解决方案,但我似乎无法掌握它。 So i am now reaching out to you guys for some help. 所以我现在正在向你们寻求帮助。

I am working in Xamarin Studio, C#, and I am trying to create a loginsystem with the help of a REST API, and i am not really sure how i save the accesstoken to a string so that i can use it in further requests. 我在Xamarin Studio,C#工作,我正在尝试使用REST API创建一个登录系统,我不确定如何将accesstoken保存到字符串,以便我可以在进一步的请求中使用它。

FILE: MainActivity.cs 文件:MainActivity.cs



namespace APItest{[Activity(Label = "APItest", MainLauncher = true)]public class MainActivity : Activity{private WebClient mClient;private Uri mUrl;private List<string> mItems;private ListView mListView;
 ` public string acesstoken;`
 ` protected override void OnCreate(Bundle savedInstanceState)` 
` {`
 ` base.OnCreate(savedInstanceState);` 
SetContentView(Resource.Layout.Main); mListView = FindViewById<ListView>(Resource.Id.myListView);
 ` mClient = new WebClient();`
 ` mUrl = new Uri("http://...link.../login");`
 ` mClient.DownloadDataAsync(mUrl);` 
mClient.Headers.Add("User", ”email@gmail.com");mClient.Headers.Add("Pass", ”myPassword”;mClient.Headers.Add("Content-Type", "application/json");mClient.Headers.Add("Accept", "application/json"); accesstoken = mClient.Headers.Get(”Accesstoken").ToString();
 ` mItems = new List<string>();`
 ` mItems.Add(accesstoken);` 
 
 ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, mItems); 
``
 
mListView.Adapter = adapter;

 }
}
 
`}


In Postman API tool (I send username and password in headers and receive following text) 在Postman API工具中(我在标题中发送用户名和密码并接收以下文本)


{
"Result": {
”mRegister": {
"allowed": 0,
”Testmsg": "**"
},
 ` …` 
"token": ”EB9TEBINlVOASM0Ok04RlIjI8JGMVNVV1smFu5MT"
}
}


I know i have a lot to learn, but i would really appreciate your help so i can get started. 我知道我有很多需要学习的东西,但我真的很感谢你的帮助,所以我可以开始了。

Thank you in advance! 先感谢您!

你有没有尝试过:

accesstoken = mClient.Headers.Get("token").ToString();

Getting token form json string. 获取令牌形式的json字符串。

Using Newtonsoft.Json library, create your response Json object. 使用Newtonsoft.Json库,创建响应Json对象。 And get the token string. 并获取令牌字符串。

Following example show how to create a User json object to get the username and password: 下面的示例演示如何创建User json对象以获取用户名和密码:

using Newtonsoft.Json;

public class MainActivity : Activity
{
    protected override async void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        BlobCache.ApplicationName = "AkavacheText";
        string json = @"{ 'Username': 'Mike','Password': 'Ma'}"; 
        SetContentView(Resource.Layout.Main);
        var getData = JsonConvert.DeserializeObject<User>(json);
        System.Console.WriteLine(getData.Username+"---"+ getData.Password);
    }
 }

public class User
{
  public  string Username { get; set; }
  public string   Password { get; set; }
}

string json should be your response string. string json应该是你的响应字符串。 User object should be your response object that structure depends on your json format. User对象应该是您的响应对象,该结构取决于您的json格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#WPF应用程序中保存accessToken - How to save an accessToken in C# WPF application 如何保存列表<string >在 Visual Studio C# 属性设置中 - How save a list<string > in visual studio c# properties setting 如何从 Xamarin C# 中的列表视图标签中的 api 获取 JSON - How to get JSON from api in listview labels in Xamarin C# 如果使用Visual Studio在Xamarin Forms C#中从远程URL读取字符串,则抛出异常 - An exception is thrown if read string from remote url in Xamarin Forms C# with Visual Studio 如何从Orange Message API获取AccessToken - How To Get AccessToken From Orange Message Api Aweber C#Api未经授权-AccessToken密钥无效 - Aweber C# Api unauthorized - AccessToken key is invalid C#:无法获取Accesstoken。 Google Calendar API - C# : Unable to obtain Accesstoken. Google calendar API 如何从列表保存内容 <string> 到C#中的文本文件? - How to save content from List<string> to a text file in C#? C#-如何从字典中保存图像 <string, images> 到文件夹 - C# - How to save images from a dictionary<string, images> to a folder 如何从 MSTest c# 代码中获取我的 Azure 应用服务的 accessToken - How to get accessToken to my Azure App Service from inside MSTest c# code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM