我将为呼叫api facebook创建应用。
当我运行我的应用程序时。它显示“未找到客户端令牌,请设置客户端令牌”。
我不确定我的代码。
我将编辑我的代码以便工作。
这是我的main Activity
(java文件)你能编辑我的代码吗?
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
EditText name, password;
ProgressDialog mDialog;
ImageView imgAvatar;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
name = (EditText) findViewById(R.id.Name);
password = (EditText) findViewById(R.id.Password);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("name", "password"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
mDialog = new ProgressDialog(MainActivity.this);
mDialog.setMessage("Loading Data");
String accesstoken = loginResult.getAccessToken().getToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
mDialog.dismiss();
Log.d("response",response.toString());
getData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("name","password");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
if(AccessToken.getCurrentAccessToken()!=null)
{
name.setText(AccessToken.getCurrentAccessToken().getUserId());
}
printKeyHash();
}
private void printKeyHash() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.thpa.a9019.testfacebook", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public int getData(JSONObject object) {
try {
URL profile_picture = new URL("https://grap.facebook.com/" + object.getString("id") + "/picture?width=250&height=250");
Picasso.with(this).load(profile_picture.toString()).into(imgAvatar);
name.setText(object.getString("name"));
password.setText(object.getString("password"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
} }
我在Stackoverflow中搜索过,但我没有回答。 如何设置客户端令牌形成我的代码。